GameObject[] GameObject.FindGameObjectsWithTag(string tag); 1. 2. CompareTag方法可以用于判断物体的Tag: bool GameObject.CompareTag(string tag); 1. 这个函数比简单的使用字符串判断句this.tag == tag要高效的多。 CompareTag最常用于碰撞的判定,如下面的例子: void OnCollisionEnter(collider other) { if(o...
public SphereCollider MyCollisionComp = null; void Start() { // 创建碰撞组件(如果还未创建的话) if (MyCollisionComp == null) { MyCollisionComp = gameObject.AddComponent<SphereCollider>(); MyCollisionComp.center = Vector3.zero; MyCollisionComp.radius = 20.0f; } } }Copy full snippet 在上面...
void OnCollisionEnter2D(Collision2D collision) { // If you want to check who you collided with, // you should typically use tags, not names. if (collision.gameObject.tag == "Platform") { // Play footsteps or a landing sound. } } 觸發器有時您想要檢測碰撞,但同時不想捲入任何物理特性。
Unity provides a number of methods to know when a collision happens or a trigger is triggered. The following shows just a basic example: XML Copy void OnCollisionEnter(Collision collision) { // Called when you have a physical collision. Debug.Log("Collided with " + collision.gameObject.name...
tag The tag of this game object. transform The Transform attached to this GameObject. hideFlags Should the object be hidden, saved with the Scene or modifiable by the user? name The name of the object. allowOcclusionWhenDynamic Controls if dynamic occlusion culling should be performed for this ...
Note: This class is almost always used when you need to draw a couple of lines or triangles, and don't want to deal with meshes. If you want to avoid surprises the usage pattern is this: using UnityEngine; using System.Collections;public class ExampleClass : MonoBehaviour { void OnPostRe...
(Collider2D collision) { if (!isAnimating) { if(gameObject.transform.position.x < collision.transform.position.x) //如果在左边 { StartCoroutine(nameof(RotateAndClock)); } else { StartCoroutine(nameof(RotateClock)); } } } private IEnumerator RotateAndClock() { isAnimating = true; for (int...
void OnCollisionEnter2D(Collision2D collision) { // If you want to check who you collided with, // you should typically use tags, not names. if (collision.gameObject.tag == "Platform") { // Play footsteps or a landing sound. } } Triggers Sometimes you want to detect a collision bu...
OnCollisionEnterAsync(); await trigger.OnCollisionEnterAsync(); // every moves. await this.GetAsyncMoveTrigger().ForEachAsync(axisEventData => { });AsyncReactiveProperty, AsyncReadOnlyReactiveProperty is UniTask's version of ReactiveProperty. BindTo extension method of IUniTaskAsyncEnumerable<T> ...
Collision包含接触点,碰撞速度等细节。如果在函数中不使用碰撞信息,省略collisionInfo参数以避免不必要的运算。注意如果碰撞体附加了一个非动力学刚体,只发送碰撞事件。可以被用作协同程序。当鼠标在GUIElement(GUI元素)或Collider(碰撞体)上点击时调用OnMouseDown。 Update 是实现各种游戏行为最常用的函数。 yield 一个...