Several functions of unity collision detection

Article directory


one,

OnCollisionEnter(Collision collision): This function is used to detect collision events between two objects and is called when two objects collide. Among them, the parameter collision contains detailed information about the collision, such as collision point, collision normal, etc.


two,

OnCollisionStay(Collision collision): This function is used to detect continuous collision events between two objects. It is called when two objects continue to collide. Among them, the parameter collision contains detailed information about the collision, such as collision point, collision normal, etc.


three,

OnCollisionStay(Collision collision): This function is used to detect continuous collision events between two objects. It is called when two objects continue to collide. Among them, the parameter collision contains detailed information about the collision, such as collision point, collision normal, etc.


Four,

OnTriggerStay(Collider other): This function is used to detect whether an object continues to stay within the trigger range of another object. It is called when an object continues to stay within the trigger range of another object. Among them, the parameter other represents the object staying in the trigger.

five,

OnCollisionExit(Collision collision): This function is used to detect the end of the collision event between two objects and is called when the collision between the two objects ends. Among them, the parameter collision contains detailed information about the collision, such as collision point, collision normal, etc.

six,

OnTriggerExit(Collider other): This function is used to detect whether an object leaves the trigger range of another object. It is called when an object leaves the trigger of another object. Among them, the parameter other represents the object leaving the trigger.


7. Code

using UnityEngine;

public class CollisionExample : MonoBehaviour
{
    
    
    // 碰撞事件,当两个物体发生碰撞时被调用
    void OnCollisionEnter(Collision collision)
    {
    
    
        Debug.Log("碰撞事件:" + collision.gameObject.name);
    }

    // 触发器事件,当一个物体进入另一个物体的触发器时被调用
    void OnTriggerEnter(Collider other)
    {
    
    
        Debug.Log("进入触发器:" + other.gameObject.name);
    }

    // 碰撞持续事件,当两个物体持续发生碰撞时被调用
    void OnCollisionStay(Collision collision)
    {
    
    
        Debug.Log("碰撞持续事件:" + collision.gameObject.name);
    }

    // 触发器持续事件,当一个物体持续停留在另一个物体的触发器内时被调用
    void OnTriggerStay(Collider other)
    {
    
    
        Debug.Log("触发器持续事件:" + other.gameObject.name);
    }

    // 碰撞结束事件,当两个物体之间的碰撞结束时被调用
    void OnCollisionExit(Collision collision)
    {
    
    
        Debug.Log("碰撞结束事件:" + collision.gameObject.name);
    }

    // 触发器结束事件,当一个物体离开另一个物体的触发器时被调用
    void OnTriggerExit(Collider other)
    {
    
    
        Debug.Log("触发器结束事件:" + other.gameObject.name);
    }
}

Guess you like

Origin blog.csdn.net/qq_20179331/article/details/129766775