📅  最后修改于: 2023-12-03 15:33:17.618000             🧑  作者: Mango
OnTriggerEnter
is a method in the MonoBehaviour
class in Unity game engine. It is used to detect when a GameObject with a Collider component enters another GameObject's trigger Collider.
void OnTriggerEnter(Collider other)
using UnityEngine;
public class MyTriggerScript : MonoBehaviour
{
void OnTriggerEnter(Collider other)
{
Debug.Log("Entered trigger!");
}
}
In this example, when some other GameObject enters the trigger which the MyTriggerScript
component is attached to, Entered trigger!
will be logged to the console.
isTrigger
set to true
.isKinematic
in order for OnTriggerEnter
to be called.OnTriggerEnter
is called on the object that has the script attached to it, not the object that entered the trigger.OnTriggerEnter
is a useful tool for detecting when GameObjects collide in Unity. It can be used for a variety of tasks, such as triggering events or activating and deactivating GameObjects.