Rigidbody player slightly phasing into wall

I have a very basic player controller script:

public class PlayerController : MonoBehaviour {

    public float moveSpeed = 1;
    private Rigidbody body;

    void Start () {
        body = gameObject.GetComponent<Rigidbody>();
    void FixedUpdate()
        body.transform.Translate(Input.GetAxis("Horizontal") * moveSpeed,
                                 Input.GetAxis("Vertical") * moveSpeed);


Mostly it works, but when I move the player (currently using a simple cube) into a wall (also simple blocks) and continue holding down the movement key, the player phases into the wall just a bit. When I release the movement key, they go back out to where they ought to be colliding with it. When phasing into walls, it’s possible to move the player through spaces way more narrow than the character is, and sometimes to glitch through walls entirely.

I’ve tried using a CharacterController component instead of a Rigidbody, but then the player won’t interact with physics objects and I get the inverse problem I’m having now: the player collides with the walls just a bit outside the wall, unless I shimmy back and forth, in which case it slowly slides forward until the player is actually up against the wall.

I’m stumped. This is an extremely basic and simple situation and I seem to be suffering from some part of the Unity physics engine I don’t understand.

I’m pretty sure this is because you’re moving your player with Translate(), which internally simply sets transform.position.

Setting transform.position essentially teleports your player into the wall. Then, PhysX takes over and tries to push your player out of the wall. This is typically a bad idea, because if the teleport distance is great enough the player can go right through.

Instead of Translate use rigidbody.AddForce():

rigidbody.AddForce(new Vector3(Input.GetAxis("Horizontal") * moveSpeed,  0.0f, Input.GetAxis("Vertical") * moveSpeed));

Since PhysX has an idea where the player is going, it’ll do better collision tests and move the player for you. Setting rigidbody.velocity might seem like a better choice at first, but it can suffers from the same issues as Translate() does. In my experience AddForce() (begrudgingly) is really the most stable method.

It’s a bit more tricky to code - especially if you’re doing cartoony movement. However, the benefits of letting PhysX do more of the heavy lifting saves a lot of headaches over that pile-of-crap character collider. Past this, if your player still tunnels (typically only a problem if you’re travelling at intense speeds), change the collision solving to Continuous Dynamic, and PhysX will use more expensive tracing calculations to ensure objects don’t pass through each other.

I would rather go with character controller. It would not pass into a wall

I’d not go with physics, it’s lame.
Instead make your wall doubled, with outer invisible shell, and let your player stick inside it, leaving visible obstacle unreached.