- Home /
Changing a collision during a collision
I'm making a 2d platformer where you can change if two objects can collide or not at runtime. My problem occurs when my player is already colliding with some object. If the player is colliding with the object (collisions enabled) and I disable the collisions at runtime, the two objects keep colliding even though they shouldn't be allowed to collide anymore. Once they stop colliding, they will continue to not collide so everything is working perfectly then. For example, my player might be standing on a platform. Collisions are enabled and everything works perfectly. I then make collisions not be enabled and my player keeps standing there despite that collisions should no longer taking place. Now, if I make my player jump, when he falls down, he'll fall through the platform. No collision is made.
I've tried fixing this in various ways. I've tried making it so that I'm changing the actual physics collisions; I've tried changing the layers; I've even made it so that a force is applied when you change these collisions. Nothing works. Any suggestions on how to fix this?
Answer by Aridez · Mar 10, 2014 at 04:25 AM
Well since there is no code I can't be sure if the problem lies here but, maybe you are using the OnCollisionEnter and then the state is only updated when the collision happens. You could use the OnCollisionStay so its updated while they are colliding. Hope this helps!
Answer by Owen-Reynolds · Mar 10, 2014 at 12:55 PM
It could be a rigidbody sleep issue. Can fix by sending rigid body.Wakeup()
to objects that might start moving.
To save time, the physics system puts boring objects on a list to ignore. For example, if a rock has rolled along the ground and stopped, there's no reason to compute friction on speed 0, then apply gravity, then cancel gravity. The semi-technical term is "sleeping." Things sleep after they've been completely at rest for a little while.
If the object gets pushed, or something knocks into it, it gets woken automatically. So you normally never have to even think about sleeping rigidbodies, or use WakeUp yourself.
But, if you do something unexpected, the system doesn't know to do the automatic wakeup. Destroying a platform will leave the stuff on it doing a "road runner" -- just hanging there. But the tiniest bump will make it fall (but not downward LookAt or raycast :-) )
I just tried using rigidbody2D.WakeUp() on my player and it hasn't fixed the problem. I logged when my player is awake and when it's sleeping. I've logged when the player and the platform can collide and when collisions aren't enabled. By all accounts, gravity should be causing the player to fall through the platform. Interestingly, if ins$$anonymous$$d of disabling the collision between the two objects, I just turn off the collision box for the platform, the player falls through. When I change the collision box to a trigger, the player falls through. But when I only change the collision matrix itself, it updates but the change doesn't really take effect until I stop the collision.
I'm pretty certain of what the problem is now. The problem is that a change to a collision matrix for 2 layers does not take place for objects colliding in those two layers until the collision stops. I've also checked and found that what's happening is that "OnCollisionStay2D" happens until you change the collision matrix (good) but "OnCollisionExit2D" never happens (bad). I assume this is related. Could this be a bug?
Bug is subjective. I think, for speed, the physics system makes certain assumptions. Like you won't change the collision matrix mid-collision. And you get a feel for when you're doing stuff where it might need a little kick afterwards. I$$anonymous$$HO, any system that always works smoothly in all cases, is dumbed down and slow.
I want to say changing your layer is safer, but I don't use 2D physics.
Your answer
Follow this Question
Related Questions
Manage multiple collider2d 1 Answer
How can I avoid collisions between the same objects? 3 Answers
On Collision sound clip change 1 Answer
Collision Problems 1 Answer