- Home /
collider doesnt work the 2nd time
I have a platform with a game object attached to it that checks when something enters it, when my player enters it, it will disable its collider and when it exits it will re enable it. what it does is disable, collide with the platform once, and when it comes back down it disables it again for some reason... i have a gameobject attached to the ball with a trigger. void OnTriggerEnter2D(Collider2D other){ player.collider2D.enabled = false;
}
void OnTriggerExit2D(Collider2D other){
player.collider2D.enabled = true;
}
the script on the platform gameObject^
You have a platform with the above script?
What is gameobject attached to the ball with a trigger you describe. Is it a seperate Object than Player and Platform?
its a child of the Player, since when the player enters the trigger of the platform it gets disabled and i can't re enable it.
Answer by Priyanshu · Mar 21, 2015 at 11:20 AM
Basically what must be happening is when the child object enters platform (OnTriggerEnter2D executes) , Player collider is disabled.
But when child exits (OnTriggerExit2D executes), Player collider is Enabled.
But when a collider is enabled inside another collider OnTriggerEnter2D is called
. This enabling of player's collider inside Platform might be disabling it again by calling OnTriggerEnter2D again..
To avoid this you must check if the Collider2D "other" does not belong to player. If does not then execute the following code.
Basically the same thing as checking if velocity>0 to disable collision and velocity<0 to enable it back again, but i was having problems with checking for velocity because sometimes the ball would get stuck in the middle of the platform since its velocity changes within platform bounds. and I didn't understand your explanation really.
Grey : Player
Pink: Child of player
Black: platform
What I understood was when 'pink' enters 'black'. Collider of 'Grey' disables.
When 'pink' exits 'black'. Collider of 'black' enables.
Correct?
When 'pink' exits 'black'. Collider of ' grey' enables.What I understood was when 'pink' enters 'black'. Collider of 'Grey' disables.
When 'pink' exits 'black'. Collider of 'black' enables.
Yes sorry i meant - When 'pink' exits 'black'. Collider of 'grey' enables.
Now: Pink enters black. Grey's collider disabled.
Pink exits black. Grey's collider enabled.
Your answer
Follow this Question
Related Questions
If trigger hit, spawn it 1 Answer
Does the "Layer Collision Matrix" affect triggers? 1 Answer
Collission and Trigger on Same object 0 Answers
Question about collision and trigger 2 Answers
OnTriggerStay still called after moving a parent gameobject 0 Answers