unity碰撞检测(耗费性能)

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

public class PengZhuang : MonoBehaviour {
    public int lingLi = 100;
    System.Timers.Timer timer = new System.Timers.Timer();
    private bool isStay = true;
	// Use this for initialization
	void Start () {
		
	}
	
	// Update is called once per frame
	void Update () {
		
	}

    void OnCollisionEnter(Collision col)
    {
        if (timer.Enabled == false) {
            timer.Interval = 1000;
            timer.Enabled = true;
            timer.Elapsed += (a, b) => stayEvent();
        }
        
        col.gameObject.GetComponent<Renderer>().material.color = Color.red;
        Debug.Log("开始碰撞" + col.collider.gameObject.name);
    }
    void OnCollisionStay(Collision col)
    {
    }
    void OnCollisionExit(Collision col)
    {
        isStay = false;
        timer.Stop();
        timer.Close();
        Debug.Log("碰撞结束" + col.collider.gameObject.name);
    }

    void stayEvent() {
        if (isStay && lingLi >=0) {
            lingLi--;
            //Vector3 v = gameObject.transform.localScale;
            //v.x = v.x * lingLi / 100;
            //v.y = v.y * lingLi / 100;
            //v.z = v.z * lingLi / 100;
            //gameObject.transform.localScale = v;
            Debug.Log("持续碰撞中");
        }
    }

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