指定此碰撞体是否配置为触发器。
触发器不会注册与传入的 Rigidbody 的碰撞。相反,当刚体进入或退出触发器体积时,它会发送 OnTriggerEnter、OnTriggerExit 和 OnTriggerStay 消息。
using UnityEngine;
public class Example : MonoBehaviour { Collider m_ObjectCollider;
void Start() { //Fetch the GameObject's Collider (make sure they have a Collider component) m_ObjectCollider = GetComponent<Collider>(); //Here the GameObject's Collider is not a trigger m_ObjectCollider.isTrigger = false; //Output whether the Collider is a trigger type Collider or not Debug.Log("Trigger On : " + m_ObjectCollider.isTrigger); }
void OnMouseDown() { //GameObject's Collider is now a trigger Collider when the GameObject is clicked. It now acts as a trigger m_ObjectCollider.isTrigger = true; //Output to console the GameObject’s trigger state Debug.Log("Trigger On : " + m_ObjectCollider.isTrigger); } }