Version: 2018.1 (switch to 2018.2b or 2017.4)
LanguageEnglish
  • C#
  • JS

Script language

Select your preferred scripting language. All code snippets will be displayed in this language.

MonoBehaviour.OnCollisionEnter2D(Collision2D)

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

Cancel

Parameters

other The Collision2D data associated with this collision.

Description

Sent when an incoming collider makes contact with this object's collider (2D physics only).

Further information about the collision is reported in the Collision2D parameter passed during the call. If you don't need this information then you can declare OnCollisionEnter2D without the parameter.

Note: Collision events will be sent to disabled MonoBehaviours, to allow enabling Behaviours in response to collisions.

See Also: Collision2D class, OnCollisionExit2D, OnCollisionStay2D.

The folllowing two script examples create a OnTriggerEnter2D demo. Example1 generates a Unity logo sprite, GameObject2. 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, OnCollisionStay2D and OnCollisionExit2D methods. These are 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 collide 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 and it stays collided for a short length of time.

#pragma strict
public class Example1 extends MonoBehaviour {
	private var bc: BoxCollider2D;
	private var rb: Rigidbody2D;
	function Awake() {
		var sprRend: SpriteRenderer = 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;
	}
	function Start() {
		gameObject.GetComponent.<SpriteRenderer>().sprite = Resources.Load.<Sprite>("_unityicon128-72");
		gameObject.transform.Translate(4.0f, 0.0f, 0.0f);
		gameObject.transform.localScale = new Vector2(2.0f, 2.0f);
	}
}
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>("_unityicon128-72"); gameObject.transform.Translate(4.0f, 0.0f, 0.0f); gameObject.transform.localScale = new Vector2(2.0f, 2.0f); } }

Example2. This animates the second sprite, GameObject2. It collides with the first sprite GameObject1.

#pragma strict
public class Example2 extends MonoBehaviour {
	var other: GameObject;
	private var spriteMove: float;
	private var stayCount: float;
	function Awake() {
		var sprRend: SpriteRenderer;
		sprRend = gameObject.AddComponent.<SpriteRenderer>() as SpriteRenderer;
		sprRend.color = new Color(0.9f, 0.9f, 0.9f, 1.0f);
		var bc: BoxCollider2D;
		bc = gameObject.AddComponent.<BoxCollider2D>() as BoxCollider2D;
		bc.size = new Vector2(1.3f, 1.3f);
		bc.isTrigger = true;
		other = GameObject.Find("GameObject1");
	}
	function Start() {
		gameObject.GetComponent.<SpriteRenderer>().sprite = Resources.Load.<Sprite>("circle");
		gameObject.transform.Translate(-4.0f, 0.0f, 0.0f);
		spriteMove = 0.1f;
	}
	function FixedUpdate() {
		gameObject.transform.Translate(spriteMove, 0.0f, 0.0f);
		if (gameObject.transform.position.x < -4.0f) {
			spriteMove = 0.1f;
		}
	}
	function OnTriggerEnter2D() {
		if (other.tag == "GameObject1") {
			stayCount = 0;
			Debug.Log("OnTriggerEnter2D");
		}
	}
	function OnTriggerStay2D() {
		Debug.Log("OnTriggerStay2D");
		stayCount = stayCount + 1;
		if (stayCount > 5) {
			spriteMove = -0.1f;
		}
	}
	function OnTriggerExit2D() {
		Debug.Log("OnTriggerExit2D");
	}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Example2 : MonoBehaviour { GameObject other;

private float spriteMove; private float stayCount;

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;

other = GameObject.Find("GameObject1"); }

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) { spriteMove = 0.1f; } }

void OnTriggerEnter2D() { if (other.tag == "GameObject1") { stayCount = 0; Debug.Log("OnTriggerEnter2D"); } }

void OnTriggerStay2D() { Debug.Log("OnTriggerStay2D"); stayCount = stayCount + 1;

if (stayCount > 5) { spriteMove = -0.1f; } }

void OnTriggerExit2D() { Debug.Log("OnTriggerExit2D"); } }

The two objects are stored as GameObjects each with one of the scripts. These GameObjects start with just the script example that it needs. Example1 is applied to GameObject1 and Example2 to GameObject2.

Did you find this page useful? Please give it a rating: