Hi I’m having issues with my player clipping into a wall.
When I walk into a wall and carry on holding the movement key, the player will slightly clip into the wall and stay there until I release the movement key. Here’s an example image:
I’ve tried to increase the size of the collider on both the player and the wall, but the same issue occurs where there is slight movement towards the wall until the key is released.
Does anyone know how to fix this problem? I only have a rigid body on the player object, and am using the MovePosition function to move the player. I also have Collision Detection set to Continuous on the rigidbody.
Based on reading some other answers, it seems that rb.MovePosition could miss collisions in some cases.
You could try adding forces to move instead, using rb.AddForce([vector3 of your position change], ForceMode.VelocityChange).
If you want to simulate a constant velocity while using a force, you can do rb.AddForce( [your position change] - rb.velocity, ForceMode.VelocityChange). This should subtract out the previous velocity, moving you at a constant speed.
If you already have movement set up with forces, you can also try going to the rigidbody of your player object, and change collision detection to “continuous”. This is a higher performance cost, but more accurate collision detection.
I could be wrong with my answer, as I haven’t made any collision detection in awhile, but I think I know the issue.
Is it possible that your player movement is in the Update function? I used Update to move my 2D character in my game, but it was clipping slighting into the tiles I placed when I moved into them.
Try placing the player movement code into void FixedUpdate() instead. That fixed it for me.