유니티 OnTriggerEnter2D - yuniti OnTriggerEnter2D

Suggest a change

Success!

Thank you for helping us improve the quality of Unity Documentation. Although we cannot accept all submissions, we do read each suggested change from our users and will make updates where applicable.

Close

Submission failed

For some reason your suggested change could not be submitted. Please <a>try again</a> in a few minutes. And thank you for taking the time to help us improve the quality of Unity Documentation.

Close

Your name Your email Suggestion*

Cancel

Switch to Manual

Parameters

other The other Collider2D involved in this collision.

Description

Sent when another object enters a trigger collider attached to this object (2D physics only).

Further information about the other collider is reported in the Collider2D parameter passed during the call.

This message is sent to the trigger Collider2D and the Rigidbody2D (if any) that the trigger Collider2D belongs to, and to the Rigidbody2D (or the Collider2D if there is no Rigidbody2D) that touches the trigger.

Note: Trigger events are only sent if one of the Colliders also has a Rigidbody2D attached. Trigger events are sent to disabled MonoBehaviours, to allow enabling Behaviours in response to collisions.

See Also: Collider2D class, OnTriggerExit2D, OnTriggerStay2D.

The following two script examples create an OnTriggerEnter2D demo. Example1 generates a Unity logo sprite, GameObject1. This sprite is collided with by the Example2 sprite, GameObject2. The Example1 script creates the Rigidbody2D. The kinematic mode is used on this script. Example2 supports the OnCollisionEnter2D method. This is called when GameObject2 collides with GameObject1. The script code for GameObject2 controls the time it takes to collide with GameObject1. GameObject2 is animated left-to-right repeatedly. When on the left side of the screen GameObject2 moves right towards GameObject1. When these have collided GameObject2 returns back to the left. The left side of the screen is the starting point for GameObject2. The right side of the screen is the constant position of GameObject1. The Example2 script code makes GameObject2 collide with GameObject1. GameObject2 stays collided for a short length of time.

Example2. This is the sprite that moves forwards and backwards and triggers with 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; } }

Suggest a change

Success!

Thank you for helping us improve the quality of Unity Documentation. Although we cannot accept all submissions, we do read each suggested change from our users and will make updates where applicable.

Close

Submission failed

For some reason your suggested change could not be submitted. Please <a>try again</a> in a few minutes. And thank you for taking the time to help us improve the quality of Unity Documentation.

Close

Your name Your email Suggestion*

Cancel

Parameters

other The other Collider2D involved in this collision.

Description

Sent when another object enters a trigger collider attached to this object (2D physics only).

Further information about the other collider is reported in the Collider2D parameter passed during the call.

Trigger events will be sent to disabled MonoBehaviours, to allow enabling Behaviours in response to collisions.

See Also: The Collider2D class and the OnTriggerExit2D and OnTriggerStay2D messages.

An OnTriggerEnter2D example is shown. This example has two empty GameObjects, called GameObject1 and GameObject2. These both have script files which makes the example work. The first script, Example1, creates a Sprite and adds a BoxCollider2D and a Rigidbody2D. This object falls under gravity and collides with Example2. Example2 has no visibility. (The rectangle it creates is visible in the Scene window.) Both GameObjects report the collision.

The script below is 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; } }

This is Example2 which is the collide script for the second 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); } }