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 | The other Collider involved in this collision. |
When a GameObject collides with another GameObject, Unity calls OnTriggerEnter.
OnTriggerEnter happens on the FixedUpdate function when two GameObjects collide. The Colliders involved are not always at the point of initial contact.
Note: Both GameObjects must contain a Collider component. One must have Collider.isTrigger enabled, and contain a Rigidbody. If both GameObjects have Collider.isTrigger enabled, no collision happens. The same applies when both GameObjects do not have a Rigidbody component.
To use the following code sample, create a primitive GameObject, and attach a Collider and Rigidbody component to it. Enable Collider.isTrigger and Rigidbody.isKinematic. When the primitive collides, OnTriggerEnter gets called. In this example, the direction immediately reverses. When the GameObject exits the collision, Unity calls OnTriggerExit, changing the color.
using UnityEngine;
public class exampleScript : MonoBehaviour { private float speed = 2f; Renderer rend; int colorPicker = 0;
private void Start() { rend = GetComponent<Renderer>(); transform.position = Vector3.zero; transform.rotation = Quaternion.Euler(0, 90, 0);
//Create two GameObjects to act as walls GameObject leftWall = GameObject.CreatePrimitive(PrimitiveType.Cube); GameObject rightWall = GameObject.CreatePrimitive(PrimitiveType.Cube); //Move the walls to the correct positions leftWall.transform.position = new Vector3(-10, 0, 0); rightWall.transform.position = new Vector3(10, 0, 0); //Scale the walls leftWall.transform.localScale = new Vector3(1, 2, 1); rightWall.transform.localScale = new Vector3(1, 2, 1); }
//moves the Primitive 2 units a second in the forward direction void Update() { transform.Translate(Vector3.forward * Time.deltaTime * speed); }
//When the Primitive collides with the walls, it will reverse direction private void OnTriggerEnter(Collider other) { speed = speed * -1; colorPicker = Random.Range(0, 10); }
//When the Primitive exits the collision, it will change Color private void OnTriggerExit(Collider other) { switch (colorPicker) { case 0: rend.material.color = Color.white; break; case 1: rend.material.color = Color.cyan; break; case 2: rend.material.color = Color.blue; break; case 3: rend.material.color = Color.black; break; case 4: rend.material.color = Color.red; break; case 5: rend.material.color = Color.green; break; case 6: rend.material.color = Color.grey; break; case 7: rend.material.color = Color.magenta; break; case 8: rend.material.color = Color.yellow; break; case 9: rend.material.color = Color.gray; break; } } }
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!