Top down 2D collision: How do I prevent players from getting "stuck" in walls?
In my game, the player moves using velocity until they run into a collider (Up, down, left or right). They cannot redirect their movement once moving. However, if they are directly touching a collider, they can move into it (again) and get "stuck" in the wall, unable to move again because it reads as moving.
Using a Raycast does not seem to work because if the player is already touching a wall, their collider is already slightly inside of it and it seems to ignore the collider. Currently, I am using a bool to check whether or not the player just moved in a specific direction, essentially preventing them from moving in the same direction 2 times in a row. However, if they run into a corner, this problem can still occur.
Is there any way to check the direction of a collision? Or a simple solution to this problem? I am in the process of learning C# and would greatly appreciate some assistance. Thanks in advance!
Try to attach Physics $$anonymous$$aterial to wall with no friction.
Hey @positive7 thanks for the quick response! I have tried that and that doesn't seem to do anything. i think that once I run into the wall, he just gets stuck and the code I have basically reads that he is moving, however, he is stationary running into the wall.
Answer by Europa012 · Sep 09, 2015 at 06:10 AM
Why not try this? Once the object hits a wall, set the movement speed to 0 from within the script. and make sure that the mass of the walls are big.. something like 99999 could to it. That is, if you don't mean them to move or anything. and if there is no gravity. Hope this helps
Answer by lloladin · Sep 24, 2015 at 09:16 AM
first of all are you using a box/colliders/cirkel/colliders?
if you are using a box colliders this should fix all youre problmes if you go to youre rigidbody and change the collision detections to continuous this will take more computer power but it will also make the collision detections work better
if you are a using a cirkel collider the problem could be that the edges of the cirkel colliders are getting stucked in walls which i personnaly solved by using a mix of box colliders and cirkel colliders to remove the edges of the top of the cirkel i can send a picture if you need
Your answer
Follow this Question
Related Questions
Player controlled platforms 0 Answers
Getting rid of 2D collision jitter 1 Answer
Help 2D Custom HillClimbRacingGame 0 Answers
How can I slow the movement of a single rigidbody2D? 2 Answers