How to make OnTriggerEnter2D work?

I’m using Unity 4.3 and I was testing the 2D physics. Somehow I can’t get a collision response from a kinetic body and a trigger.

Here’s the basic setup:

Player
  +- RigidBody2D (kinematic)
  +- CircleCollider2D

Trigger
  +- CircleCollider2D (trigger)

There’s a OnTriggerEnter2D method on both the Trigger- and the Player-Script, but they don’t fire.

If I ditch the 2D physics components and build the exact same setup with the old (3D) physics components, everything works as expected.

i’ve sent a bug report and they answered me they admit the issue and will send the ticket to developers.

In my trial and error tests, it seems as though kinematic rigidbody triggers can’t collide in 2D mode. I got it to fire off an OnTriggerEnter2D by turning off “Is Kinematic”.

You should read the docs. Rigidbody2D.isKinematic

If this property is set to true then the rigidbody will stop reacting to collisions and applied forces.

Which clearly means, that a kinematic rigidbody is the same as if you didn’t have a rigidbody at all. Although, at Rigidbody.isKinematic, the docs say

If isKinematic is enabled, Forces, collisions or joints will not affect the rigidbody anymore.

which, on the other hand, means, that the rigidbody will not respond to collision by moving away, but still trigger OnTriggerEnter, etc.

The solution?

Set the rigidbody to non-kinematic and use this script.

#pragma strict

private var g : float;

function OnEnable () {
	g = rigidbody2D.gravityScale;
	rigidbody2D.gravityScale = 0;
}

function Update () {
	rigidbody2D.velocity = Vector3.zero;
	rigidbody2D.angularVelocity = 0;
}

function OnDisable () {
	rigidbody2D.gravityScale = g;
}

With this, it won’t move or rotate, but still trigger collision calls.

Edit: Also, if you want the rigidbody to move again, just disable this script instead of disabling isKinematic.

Edit 2: It seems like gravityScale is applied after any other rigidbody calls, so if the rigidbody2D had gravity, the script wouldn’t stop it’s movement entirely, so I updated the script so that it disables the gravity while it’s enabled.

–David

Meantime before Unity solve that bug
you can simulate 3D in 2D that’s working:
Player (moving) : add Sprite Renderer.
add RigidBody with IsKinematic On (not RigidBody2D).
add BoxCollider (not BoxCollider2D) IsTrigger On or Off.

Trigger (wall) : add Sprite Renderer.
add BoxCollider with Is Trigger On

On the Script use following:

void OnTriggerEnter(Collider other)
		{
			if(other.gameObject.name =="Wall")
			{
				Debug.Log ("Trigger "+ other.gameObject.name);
			}
		}

The OnTriggerEnter2D method is never called in my game.

I got 2 sprites with Box collider 2D. The Is Trigger property is set to true but there is no collision detection.
I don’t have any RigidBody2D component.

	void OnTriggerEnter2D(Collider2D other) {
		Debug.Log (other);
		
	}
	void OnTriggerExit2D(Collider2D other) {
		Debug.Log (other);
	}

it works

function OnTriggerEnter2D(other : Collider2D)
{
if(other.tag==“passing”)
{
Score+=1;
guiScore.text="Score : "+Score;
}
}

I got it to work this way:

Player
    +- RigidBody2D (IsKinematic : see note below)
    +-  CircleCollider2D (IsTrigger : no)


Object
    +- RigidBody2D (IsKinematic : no)
    +- CircleCollider2D (IsTrigger : yes)

note: if Player IsKinematic,
OnTriggerEnter2D(Collider2D) will be fired on the player

If player is Not Kinematic,
OnCollisionEnter2D(Collision2D) will be fired on the player

I switch between kinematic and non kinematic in my script for different game play modes.

Hi Guys,

Afters lots of R&D and hardwork… i got this.

Do the following:
Player
± RigidBody2D (kinematic) is kinematics true…
± CircleCollider2D

Trigger
± CircleCollider2D (trigger) (IsTrigger == true) i.e checkbox selected in inspector.

Function
OnTriggerEnter2D(Collider2D other)

Will get called. both object having rigidbody2d will not collide in gameview.

Solved!!!

I faced the same problem but solved it by myself with a simple trick!
Follow the steps :

  1. Keep your object\image in moving continuously to use “OnTriggerEnter2D” function perfectly !
    Suppose your object\image is remain still in a certain position.
    now get the X(horizontal) value of your image\object from “Start” function like below :

  2. Now create a function to increase this X(horizontal) value continuously like below :

  3. Now call the fucntion from the "Update () " function :

    var temp : float = 0;

    function Start () {

                temp = Image1.position.x;
                
    }
    
    function KeepMoving()
        {
           Image1.position.x = temp + 0.00001; // increase the x value slightly!
           Image1.position.x = temp; // again move the fast position
        }
    
    function Update () {
        
          keepMoving();
        
         }