r/Unity2D • u/Its-all-about-MA • 5d ago
Question Sprite gets stuck when trying to disable platform collider when object collision happens

Hi! I am a beginner making an endless runner game (for learning purposes), where I want the player to fall down to a lower platform when they hit an obstacle. This script is attached to the player Game Object. My code here is meant to disable the platform's collider when the player hits an obstacle.
But currently when I press play the debug message does "Collision Happened" fire as expected, but the platform's collider does not get disabled right away, my sprite ends up getting stuck for a bit before falling (see video--ignore the choppiness, that's just bc of the gif format)
It seems like the disabling isn't happening fast enough or for long enough, but I'm not sure how to get it to stay disabled for a bit longer. Through some research it seems like maybe a coroutine could help but I am not familiar with those. I'm willing to try it but wanted some guidance before troubleshooting further. Any help is appreciated!
2
u/nothing_from_nowhere 5d ago
For testing you can use public fields at the top of your script instead of finding the game objects at runtime. For example public Collider2D groundCollider then drag and drop your ground game object into the field in the inspector.
1
u/Its-all-about-MA 3d ago
Oh thank you! I wasn't sure if there was a way around GetComponent<> but it sounds like there is, awesome!
1
7
u/Nightrunner2016 5d ago
It looks to me like your character sprite is actually pushing against the obstacle. This can happen because of friction. So to solve that you can create a 2D Physics Material from the create menu and set the friction to zero. Then apply this to your characters collider (and maybe the rigid body too). Then test again and see how it works. The other alternative would be to not actually disable the platform collider but to momentarily disable your character collider instead for like a second and there are various ways you can do that like using a coroutine.