Thank you for helping us improve the quality of Unity Documentation. Although we cannot accept all submissions, we do read each suggested change from our users and will make updates where applicable.
CloseFor some reason your suggested change could not be submitted. Please <a>try again</a> in a few minutes. And thank you for taking the time to help us improve the quality of Unity Documentation.
Closeother | A Collider involved in this collision. |
OnTriggerEnter is called when the GameObject collides with another GameObject.
The given other
Collider has details about the trigger event, such as the name of its GameObject. Either of the two GameObjects must have a Rigidbody component. The Rigidbody component has both Rigidbody.useGravity and Rigidbody.isKinematic set to false
. These prevents the GameObject from falling under gravity and having kinematic behavior. One Collider has Collider.isTrigger set to true
. The GameObject with Collider.isTrigger set to true
has OnTriggerEnter called when the other GameObject touches or passes through it. OnTriggerEnter occurs after FixedUpdate ends.
A disabled GameObject receives the OnTriggerEnter message.
using UnityEngine;
// MonoBehaviour.OnTriggerEnter example. // // A cube is based in the center of the world. A small white sphere is added // to the world and it can move anywhere in the Scene. The cube changes // color each time the sphere enters the cube. As the sphere leaves the cube, // the cube reverts back to white.
public class Example : MonoBehaviour { private float moveSpeed = 3.0f; private GameObject sphere;
void Awake() { // Create the ground. GameObject ground = GameObject.CreatePrimitive(PrimitiveType.Quad); ground.transform.rotation = Quaternion.Euler(90, 0, 0); ground.transform.localScale = new Vector3(6, 6, 6); ground.transform.position = new Vector3(0, -0.5f, 0);
// Make sure the BoxCollider is a trigger. BoxCollider boxCollider = gameObject.GetComponent<BoxCollider>(); boxCollider.isTrigger = true;
// Create a sphere to interact with this cube. sphere = GameObject.CreatePrimitive(PrimitiveType.Sphere); sphere.gameObject.transform.position = new Vector3(1f, 0, 0); sphere.gameObject.transform.localScale = new Vector3(0.5f, 0.5f, 0.5f);
// Add a Rigidbody to the sphere. // The sphere does not touch the ground. sphere.gameObject.AddComponent<Rigidbody>(); sphere.gameObject.GetComponent<Rigidbody>().useGravity = false; }
void Update() { // Move the sphere based on a/d and s/w. Transform sphereTransform = sphere.gameObject.transform; sphereTransform.Translate(Vector3.forward * Time.deltaTime * Input.GetAxis("Vertical") * moveSpeed); sphereTransform.Translate(Vector3.right * Time.deltaTime * Input.GetAxis("Horizontal") * moveSpeed);
LimitSphere(sphereTransform); }
private void OnTriggerEnter(Collider other) { // Change the cube color to green. MeshRenderer meshRend = GetComponent<MeshRenderer>(); meshRend.material.color = Color.green; Debug.Log(other.name); }
private void OnTriggerExit(Collider other) { // Revert the cube color to white. MeshRenderer meshRend = GetComponent<MeshRenderer>(); meshRend.material.color = Color.white; }
// Keep sphere inside a xz square of 2 units. private void LimitSphere(Transform sphereTransform) { if (sphereTransform.position.x < -1.0f) { sphereTransform.position = new Vector3(-1.0f, 0.0f, sphereTransform.position.z); }
if (sphereTransform.position.x > 1.0f) { sphereTransform.position = new Vector3(1.0f, 0.0f, sphereTransform.position.z); }
if (sphereTransform.position.z < -1.0f) { sphereTransform.position = new Vector3(sphereTransform.position.x, 0.0f, -1.0f); }
if (sphereTransform.position.z > 1.0f) { sphereTransform.position = new Vector3(sphereTransform.position.x, 0.0f, 1.0f); } } }
Did you find this page useful? Please give it a rating:
Thanks for rating this page!
What kind of problem would you like to report?
Thanks for letting us know! This page has been marked for review based on your feedback.
If you have time, you can provide more information to help us fix the problem faster.
Provide more information
You've told us this page needs code samples. If you'd like to help us further, you could provide a code sample, or tell us about what kind of code sample you'd like to see:
You've told us there are code samples on this page which don't work. If you know how to fix it, or have something better we could use instead, please let us know:
You've told us there is information missing from this page. Please tell us more about what's missing:
You've told us there is incorrect information on this page. If you know what we should change to make it correct, please tell us:
You've told us this page has unclear or confusing information. Please tell us more about what you found unclear or confusing, or let us know how we could make it clearer:
You've told us there is a spelling or grammar error on this page. Please tell us what's wrong:
You've told us this page has a problem. Please tell us more about what's wrong:
Thanks for helping to make the Unity documentation better!