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); } } }