Hello,
I have one Player with RigidBody2D and a pillar with BoxCollider2D.
I check when OnTriggerEnter2D is called ( Player collided with the pillar ) then I try to keep Player position by change his bodyType to Static.
When OnTriggerExit2D is called, Player bodyType will be reverted to Dynamic again.
My problem is: after change player bodyType to Static , OnTriggerExit2D is called immediately. And after reverted to Dynamic, then OnTriggerEnter2D is called immediately. It goes on repeating again and again.
This is my Player and pillar
If you have any suggest please tell me.
Thank you.
So you are aware, changing the body-type isn’t just a simple property; it has huge side-effects to a physics system. You are completely changing its dynamics and what contacts are valid. Doing this in the 2D physics system results in all existing contacts being re-evaluated. It’s a non-trivial thing and isn’t something like changing the colour of a Sprite.
By changing a body-type to Static, you’re invalidating contacts so they get destroyed hence “Exit” state. If new ones are created then they’ll result in an “Enter”.
Really, don’t go changing body-type like this then expect it not to affect contacts. I don’t follow why you’re doing what you’re doing either. Maybe you could explain a little more.
1 Like
Thanks for your comment, I’m new in Unity and I find out the way to keep Player sticks on the pillar by freeze the X and Y positions as well as the Z rotation whenever OnCollisionEnter() was triggered.