- Home /
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.
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
Answer by Mike99 · Nov 18, 2013 at 08:43 PM
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.
Answer by Liens · Nov 15, 2013 at 11:45 PM
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 $$anonymous$$m made that change or if that is a bug. Sorry for bad english. I'm french.
Answer by DavidDebnar · Nov 17, 2013 at 02:41 PM
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 Is$$anonymous$$inematic exactly describe my use-case.
Well, unless it's a bug on Unity's part, my answer is the solution.
According to the documentation (table at bottom), a $$anonymous$$inematic Rigidbody Trigger Collider vs. $$anonymous$$inematic Rigidbody Trigger Collider collision should send a Trigger $$anonymous$$essage.
@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.
Answer by akenaton · Nov 18, 2013 at 11:51 PM
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);
}
}
Answer by BenoitFreslon · Dec 02, 2013 at 11:01 PM
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);
}
Your answer
Follow this Question
Related Questions
Unity 4.3 Physics 2d Change Axis 1 Answer
Can't change Rigidbody2D mass 1 Answer
Android app size after installation, 4.3 is 2 times larger than 4.2 0 Answers
Unity 4.3.4 iOS Warnings - iAD and Asset Catalog 0 Answers
One way platform using 2D Colliders? 11 Answers