Legacy Documentation: Version 2018.2 (Go to current version)
LanguageEnglish
  • C#

Collider.OnTriggerEnter(Collider)

Suggest a change

Success!

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.

Close

Submission failed

For 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.

Close

Cancel

Parameters

otherThe other Collider involved in this collision.

Description

OnTriggerEnter is called when the Collider other enters the trigger.

This message is sent to the trigger Collider and the Rigidbody (if any) that the trigger Collider belongs to, and to the Rigidbody (or the Collider if there is no Rigidbody) that touches the trigger.

Notes: Trigger events are only sent if one of the Colliders also has a Rigidbody attached. Trigger events will be sent to disabled MonoBehaviours, to allow enabling Behaviours in response to collisions. OnTriggerEnter occurs on the FixedUpdate after a collision. The Colliders involved are not guaranteed to be at the point of initial contact.

Note: OnTriggerEnter is not technically part of Collision. It is a MonoBehaviour function.

The following CS example shows how a trigger can interact with a sphere GameObject. This GameObject is created in Awake. OnTriggerEnter, OnTriggerStay and OnTriggerExit are called when this cube interacts with the sphere. Update moves the cube forward/back and left/right.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Example : MonoBehaviour { public bool enter = true; public bool stay = true; public bool exit = true; public float moveSpeed;

void Awake() { // move and reduce the size of the cube transform.position = new Vector3(0, 0.25f, 0.75f); transform.localScale = new Vector3(0.1f, 0.1f, 0.1f);

// add isTrigger var boxCollider = gameObject.AddComponent<BoxCollider>(); boxCollider.isTrigger = true;

moveSpeed = 1.0f;

// create a sphere for this cube to interact with GameObject sphere = GameObject.CreatePrimitive(PrimitiveType.Sphere); sphere.gameObject.transform.position = new Vector3(0, 0, 0); sphere.gameObject.AddComponent<Rigidbody>(); sphere.gameObject.GetComponent<Rigidbody>().isKinematic = true; sphere.gameObject.GetComponent<Rigidbody>().useGravity = false; }

void Update() { transform.Translate(Vector3.forward * Time.deltaTime * Input.GetAxis("Vertical") * moveSpeed); transform.Translate(Vector3.right * Time.deltaTime * Input.GetAxis("Horizontal") * moveSpeed); }

private void OnTriggerEnter(Collider other) { if (enter) { Debug.Log("entered"); } }

// stayCount allows the OnTriggerStay to be displayed less often // than it actually occurs. private float stayCount = 0.0f; private void OnTriggerStay(Collider other) { if (stay) { if (stayCount > 0.25f) { Debug.Log("staying"); stayCount = stayCount - 0.25f; } else { stayCount = stayCount + Time.deltaTime; } } }

private void OnTriggerExit(Collider other) { if (exit) { Debug.Log("exit"); } } }
using UnityEngine;

public class Example : MonoBehaviour { // Destroy everything that enters the trigger void OnTriggerEnter(Collider other) { Destroy(other.gameObject); } }
对文档有任何疑问,请移步至开发者社区提问,我们将尽快为您解答