Мои Уведомления
Привет, !
Мой Аккаунт Мои Финансы Мои Подписки Мои Настройки Выход
Руководство API скрипты

Collider2D.OnTriggerEnter2D(Collider2D)

Параметры

other The other Collider2D involved in this collision.

Описание

Отправляется, когда другой объект входит в триггерный коллайдер, прикрепленный к этому объекту (только 2D-физика).

Дополнительная информация о другом коллайдере сообщается в параметре Collider2D, передаваемом во время вызова.

Триггерные события будут отправлены отключенным объектам MonoBehaviour, чтобы можно было включить Behaviour в ответ на коллизии.

Посмотрите так же: класс Collider2D и OnTriggerExit2D и сообщения OnTriggerStay2D.

Показан пример OnTriggerEnter2D. В этом примере есть два пустых GameObject с именами GameObject1 и GameObject2. У них обоих есть файлы сценариев, благодаря которым пример работает. Первый скрипт, Example1, создает Sprite и добавляет BoxCollider2D и Rigidbody2D. Этот объект падает под действием силы тяжести и сталкивается с Example2. Example2 не отображается. (Прямоугольник, который он создает, виден в окне сцены.) Оба GameObject сообщают о столкновении.

Приведенный ниже сценарий – пример 1.

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:

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); } }
Вы можете отблагодарить автора, за перевод документации на русский язык. ₽ Спасибо
API скрипты 2021.3