other | 该碰撞中涉及 Collider。 |
GameObject 与另一 GameObject 碰撞时,将调用 OnTriggerEnter。
给定的其他
Collider 具有关于触发器事件的详细信息(例如其 GameObject 的名称)。两个 GameObject 之一必须具有 Rigidbody 组件。Rigidbody 组件将 Rigidbody.useGravity 和 Rigidbody.isKinematic 都设置为 /false/。这些设置可防止 GameObject 在重力作用下坠落和具有运动行为。一个 Collider 将 Collider.isTrigger 设置为 /true/。Collider.isTrigger 设置为 true
的 GameObject 在有其他 GameObject 接触或者穿过时,将调用 OnTriggerEnter。在 FixedUpdate 结束后,将发生 OnTriggerEnter。
已禁用的 GameObject 收到 OnTriggerEnter 消息。
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:
Thank you for helping to make the Unity documentation better!
Your feedback has been submitted as a ticket for our documentation team to review.
We are not able to reply to every ticket submitted.