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.

same here...

Yes i get the same problem In 3D mode i can use kinematic rigidbody which fire OnTriggerEnter In 2D mode rigibody2D kinematic doesn't fire OnTriggerEnter2D

9 Answers

9

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

That was the case. In the meantime the bug was fixed and it works as expected now.

I still see the same behavior in Unity 4.3.4 - this isn't fixed. Can you point to release notes that claim a fix has been implemented?

It still happens for me. I'm curious what the fix is as well.

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

Any news on a fix for this? http://issuetracker.unity3d.com/issues/ontriggerstay2d-is-called-not-properly -- says "Fixed in a future release" -- is that going to be 5.0 or have they not said? I really need OnTriggerEnter2D, and need it to fire only once. OnTriggerExit2D doesn't seem to work either, so setting flag in Enter and clearing on Exit doesn't work.

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”.

Yeah I saw that too.. But what if you need the body to be kinematic?

But in unity 3D (4.2) rigidbody (3D) Is kinematic fires OnTriggerEnter and physics does not apply.That's what i want. rigidbody2D Is kinematic does not fire OnTriggerEnter2D As my game was working the way i want in 4.2 (3D) i was supposing to work the same way in 4.3 2D. i would like to know why unity team made that change or if that is a bug. Sorry for bad english. I'm french.

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

This is not a solution, this is a hack. The manual clearly states that kinematic bodies should react to triggers. In fact, the section about Rigidbodies and IsKinematic exactly describe my use-case.

Well, unless it's a bug on Unity's part, my answer is the solution.

According to [the documentation][1] (table at bottom), a Kinematic Rigidbody Trigger Collider vs. Kinematic Rigidbody Trigger Collider collision should send a Trigger Message. [1]: http://docs.unity3d.com/Documentation/Components/class-BoxCollider.html

@Liens, it doesn't say anything about the 2D box collider though, so it's irrelevant.

2d and 3d should not be different if they use the same words. A rigidbody should always be a rigidbody, and setting the kinematic value should always have the same effect.

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;
}
}

It works now, because they have fixed the bug

Still not working at me.

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();
        
         }