[Unity]刚体碰撞检测刚体反弹次数

 

2个物体必须都有Collider组件。其中一个物体必须有Rigidbody组件,IsKinematic为否。

 


    private int NumberCollisions;
    // Use this for initialization
    void Start () {
        NumberCollisions = 0;
    }
    private void OnCollisionEnter(Collision collision)
    {
        Debug.Log("    collision:" + collision.transform.name);
        NumberCollisions = NumberCollisions + 1;

        NumSave.instatice._num = NumberCollisions;


    }

 

NumberCollisions 即刚体碰撞的次数。

 

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

public class NumSave : MonoBehaviour {
    #region Singleton
    public static NumSave instance;
    private void Awake()
    {
        instance = this;
    }
    #endregion

public int _num;
}



 


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

public class Test : MonoBehaviour {
    

public void GetCollisionNum()
{

Debug.log("     num:"+NumSave.instance._num);
}



}

 

 

 

 

 

你可能感兴趣的:(Unity)