UnityAPI手册-2019.4官网版
文集大纲加载中……
本文档使用 MrDoc 发布
-
+
首页
Collider2D.OnTriggerEnter2D(Collider2D)
# [Collider2D](https://docs.unity3d.com/cn/2019.4/ScriptReference/Collider2D.html).OnTriggerEnter2D(Collider2D) ## 参数 | other | 该碰撞中涉及的其他 [Collider2D](https://docs.unity3d.com/cn/2019.4/ScriptReference/Collider2D.html)。 | | ------- | ---------------------------------------------------------------------------------------------------- | ## 描述 当另一个对象进入附加到该对象的触发碰撞体时发送(仅限 2D 物理)。 在调用期间传入的 [Collider2D](https://docs.unity3d.com/cn/2019.4/ScriptReference/Collider2D.html) 参数中报告有关其他碰撞体的进一步信息。 触发器事件将发送到已禁用的 [MonoBehaviour](https://docs.unity3d.com/cn/2019.4/ScriptReference/MonoBehaviour.html)s,以便允许启用 [Behaviour](https://docs.unity3d.com/cn/2019.4/ScriptReference/Behaviour.html)s,以响应碰撞。 请参阅:[Collider2D](https://docs.unity3d.com/cn/2019.4/ScriptReference/Collider2D.html) 类以及 [OnTriggerExit2D](https://docs.unity3d.com/cn/2019.4/ScriptReference/Collider2D.OnTriggerExit2D.html) 和 [OnTriggerStay2D](https://docs.unity3d.com/cn/2019.4/ScriptReference/Collider2D.OnTriggerStay2D.html) 消息。 此处显示了一个 [OnTriggerEnter2D](https://docs.unity3d.com/cn/2019.4/ScriptReference/Collider2D.OnTriggerEnter2D.html) 示例。此示例有两个空的 [GameObject](https://docs.unity3d.com/cn/2019.4/ScriptReference/GameObject.html),分别名为 `GameObject1` 和 /GameObject2/。这两个游戏对象都有脚本文件能使本示例正常工作。第一个脚本是 /Example1/,用于创建 [Sprite](https://docs.unity3d.com/cn/2019.4/ScriptReference/Sprite.html) 并添加 [BoxCollider2D](https://docs.unity3d.com/cn/2019.4/ScriptReference/BoxCollider2D.html) 和 [Rigidbody2D](https://docs.unity3d.com/cn/2019.4/ScriptReference/Rigidbody2D.html)。此对象受重力下落并与 `Example2` 发生碰撞。`Example2` 无法看到显示效果。(它创建的矩形在 Scene 窗口中可见。)这两个 [GameObject](https://docs.unity3d.com/cn/2019.4/ScriptReference/GameObject.html) 都会报告碰撞。 以下脚本是 Example1。 ``` using System.Collections; using System.Collections.Generic; using UnityEngine; // Create GameObject1 that falls under gravity. It will pass through // Example2 and cause a collision. GameObject1 is moved back to // the start position and it will again start to fall under gravity. public class Example1 : MonoBehaviour { void Awake() { SpriteRenderer sr; sr = gameObject.AddComponent<SpriteRenderer>() as SpriteRenderer; sr.color = new Color(0.9f, 0.9f, 0.1f, 1.0f); BoxCollider2D bc; bc = gameObject.AddComponent<BoxCollider2D>() as BoxCollider2D; bc.size = new Vector2(1.0f, 1.0f); Rigidbody2D rb; rb = gameObject.AddComponent<Rigidbody2D>() as Rigidbody2D; rb.gravityScale = 1.0f; // A square in the Resources folder is used. gameObject.GetComponent<SpriteRenderer>().sprite = Resources.Load<Sprite>("square"); // GameObject1 starts 3 units in the Up direction. gameObject.transform.position = new Vector3(0.0f, 3.0f, 0.0f); gameObject.transform.localScale = new Vector3(0.5f, 0.5f, 1.0f); } private float timer = 0.0f; private bool restart = false; void FixedUpdate() { if (restart == true) { timer = timer + Time.deltaTime; if (timer > 0.25f) { gameObject.transform.position = new Vector3(0.0f, 3.0f, 0.0f); gameObject.GetComponent<Rigidbody2D>().velocity = new Vector2(0.0f, 0.0f); restart = false; } } } // called when this GameObject collides with GameObject2. void OnTriggerEnter2D(Collider2D col) { Debug.Log("GameObject1 collided with " + col.name); restart = true; timer = 0.0f; } } ``` 这是 /Example2/,即第二个 [GameObject](https://docs.unity3d.com/cn/2019.4/ScriptReference/GameObject.html) 的碰撞脚本: ``` using System.Collections; using System.Collections.Generic; using UnityEngine; // Create a rectangle that the other GameObject will collide with. // Note that this GameObject has no visibility. // (View in the Scene view to see this GameObject.) public class Example2 : MonoBehaviour { void Awake() { BoxCollider2D bc; bc = gameObject.AddComponent<BoxCollider2D>() as BoxCollider2D; bc.size = new Vector2(3.0f, 1.0f); bc.isTrigger = true; gameObject.transform.localScale = new Vector3(3.0f, 1.0f, 1.0f); gameObject.transform.position = new Vector3(0.0f, -2.0f, 0.0f); } void OnTriggerEnter2D(Collider2D col) { Debug.Log("GameObject2 collided with " + col.name); } } ```
da
2022年5月25日 10:43
转发文档
收藏文档
上一篇
下一篇
手机扫码
复制链接
手机扫一扫转发分享
复制链接
关于 MrDoc
觅思文档MrDoc
是
州的先生
开发并开源的在线文档系统,其适合作为个人和小型团队的云笔记、文档和知识库管理工具。
如果觅思文档给你或你的团队带来了帮助,欢迎对作者进行一些打赏捐助,这将有力支持作者持续投入精力更新和维护觅思文档,感谢你的捐助!
>>>捐助鸣谢列表
微信
支付宝
QQ
PayPal
Markdown文件
分享
链接
类型
密码
更新密码