other | 该碰撞中涉及的其他 Collider2D。 |
当另一个对象进入附加到该对象的触发碰撞体时发送(仅限 2D 物理)。
在调用期间传入的 Collider2D 参数中报告有关其他碰撞体的进一步信息。
此消息会发送到触发器 Collider2D 和触发器 Collider2D 所属的 Rigidbody2D(如果有),以及接触该触发器的 Rigidbody2D(或 Collider2D,如果没有 Rigidbody2D)。
注意:如果其中一个 Collider 还附加了 Rigidbody2D,则仅发送触发器事件。触发器事件会发送到已禁用的 MonoBehaviours,以便允许启用 Behaviours 以响应碰撞。
另请参阅:Collider2D 类、OnTriggerExit2D、OnTriggerStay2D。
以下两个脚本示例创建一个 OnTriggerEnter2D 演示。Example1 生成
一个 Unity 徽标精灵 GameObject1
。此精灵与 Example2 精灵
GameObject2
碰撞。Example1 脚本创建 Rigidbody2D。在此脚本中使用了
运动学模式。Example2 支持 OnCollisionEnter2D 方法。
当 GameObject2 与
GameObject1
碰撞时,会调用此方法。GameObject2
的脚本代码控制与
GameObject1
碰撞花费的时间。GameObject2
反复地从左到右进行动画处理。当位于屏幕
左侧时,GameObject2
向右移向 GameObject1
。发生碰撞后,GameObject2
会返回到左侧。屏幕左侧是 GameObject2
的起点。
屏幕右侧是 GameObject1
的恒定位置。Example2 脚本
代码使 GameObject2
与 GameObject1
碰撞。GameObject2
保持碰撞状态较短的时间。
using System.Collections; using System.Collections.Generic; using UnityEngine;
public class Example1 : MonoBehaviour { private BoxCollider2D bc; private Rigidbody2D rb;
void Awake() { SpriteRenderer sprRend = gameObject.AddComponent<SpriteRenderer>() as SpriteRenderer; sprRend.color = new Color(0.9f, 0.9f, 0.9f, 1.0f);
bc = gameObject.AddComponent<BoxCollider2D>() as BoxCollider2D; bc.size = new Vector2(1.3f, 1.3f); bc.isTrigger = true;
rb = gameObject.AddComponent<Rigidbody2D>() as Rigidbody2D; rb.bodyType = RigidbodyType2D.Kinematic; }
void Start() { gameObject.GetComponent<SpriteRenderer>().sprite = Resources.Load<Sprite>("logo"); gameObject.transform.Translate(4.0f, 0.0f, 0.0f); gameObject.transform.localScale = new Vector2(2.0f, 2.0f); } }
Example2 是前后移动并由 Example1 触发的精灵。
using System.Collections; using System.Collections.Generic; using UnityEngine;
public class Example2 : MonoBehaviour { private float spriteMove;
void Awake() { SpriteRenderer sprRend; sprRend = gameObject.AddComponent<SpriteRenderer>() as SpriteRenderer; sprRend.color = new Color(0.9f, 0.9f, 0.9f, 1.0f);
BoxCollider2D bc; bc = gameObject.AddComponent<BoxCollider2D>() as BoxCollider2D; bc.size = new Vector2(1.3f, 1.3f); bc.isTrigger = true; }
void Start() { gameObject.GetComponent<SpriteRenderer>().sprite = Resources.Load<Sprite>("circle"); gameObject.transform.Translate(-4.0f, 0.0f, 0.0f); spriteMove = 0.1f; }
void FixedUpdate() { gameObject.transform.Translate(spriteMove, 0.0f, 0.0f);
if (gameObject.transform.position.x < -4.0f) { // move GameObject2 to the right spriteMove = 0.1f; } }
// when the GameObjects collider arrange for this GameObject to travel to the left of the screen void OnTriggerEnter2D(Collider2D col) { Debug.Log(col.gameObject.name + " : " + gameObject.name + " : " + Time.time); spriteMove = -0.1f; } }
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.