- Home /
OnTriggerEnter2D working with non-trigger collider
Hi,
I am actually working in an action/platformer.
I prototyped a falling platform, but it didn't work as expected. My platform is composed of a single game object, with two colliders: the first one is used to act like a proper collider, and the second one, a trigger collider, is a small box on top of the "normal" collider. I did that so the platform did not self-destroy if the player hit it from behind.
I have also a script attach to it, which will launch a Coroutine if the function OnTriggerEnter2D is called.
void OnTriggerEnter2D(Collider2D other)
{
if (other.tag == "Player")
{
//Animation
StartCoroutine(DestroyAndReconstruct());
}
}
My player's collider is a trigger. After some debug, it seems that the function OnTriggerEnter2D is called when the player hits the non-trigger collider. It happens even when I remove all trigger from my platform.
Did I do anything wrong?
Answer by Novemb3r · Jul 14, 2014 at 06:11 PM
When you have a trigger on either object that is colliding, OnTriggerEnter functions from any script that is attached to EITHER object get called. So since your player is a trigger, when the player makes contact with the collider you call OnTriggerEnter on both your player's scripts AND the scripts on your platform.
Your new problem would then be figuring out how to detect which side of the box you collided on so that you can execute your code only when it contacts from whatever side you want it to.
There is the simple option where you make two objects connected into one GameObject with different colliders and only one of those objects has the coroutine inducing script (which would need to be edited to function on both objects).
Or probably a better solution would be to look into raycasting and using that to determine the sides your character hit from.
This is a link to another question that explains how to use raycasting for the solution:
Answer by alwayscodeangry · Jul 14, 2014 at 05:44 PM
See this answer for details, but basically you can't have two colliders on the same object act differently.
Try making a child object with the trigger and associated script.
Your answer
Follow this Question
Related Questions
2D Physics and Jump-Through Platforms 2 Answers
Unity colliders bug (2d box) 2 Answers
projectiles not coliding with character 3 Answers
Pixel Perfect 2D Polygon Collider 2 Answers
Raycast 2D not working as i hoped 0 Answers