模拟灼烧攻击

攻击的物体(比如火焰)跟被攻击者接触时持续掉血,不接触中断掉血,再次接触继续持续掉血

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class PengZhuang : MonoBehaviour {
    public int hp = 10;
    System.Timers.Timer timer = new System.Timers.Timer(1000);
	// Use this for initialization
	void Start () {
        timer.Elapsed += (a, b) => stayEvent();
    }
	
	// Update is called once per frame
	void Update () {
		
	}

    void OnCollisionEnter(Collision col)
    {
        if (timer.Enabled == false) {
            timer.Enabled = true;
        }
        Debug.Log("开始碰撞");
    }
    void OnCollisionStay(Collision col)
    {
        //Debug.Log("持续碰撞中");
    }
    void OnCollisionExit(Collision col)
    {
        timer.Stop();
        timer.Close();
        Debug.Log("碰撞结束");
    }

    void stayEvent() {
        if (hp > 0) {
            hp--;
            print(hp);
            if (hp <= 0) {
                print("hp为0");
            }

        }
    }

}

原文地址:https://www.cnblogs.com/xiaoguangit/p/11284060.html