Player sliding keeps getting stuck with walls

I have a 3D character with a Rigidbody, and his only movement is sliding through walls (without gravity envolved) but most of the times, once it jumps from one wall to another, it collides by 1 pixel.

In the image, the player went from corner right to left, and went back from left to right, but stopped in the start of the wall before getting into the corner.
7399367--903980--upload_2021-8-8_9-56-46.png

Movement code (just in case):

using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

[RequireComponent(typeof(Rigidbody))]
public class PlayerMovement : MonoBehaviour
{
    private Rigidbody _rbody;
    public float movementSpeed = 5f;

    private Vector3 _directionalInput;
    private bool IsMoving => _rbody.velocity != Vector3.zero;
    private Vector3 _queueMovement = Vector3.zero;
    [SerializeField] private float queueMovementCooldown = 0.15f;
    private float _queueMovementTimer;
    private void Awake()
    {
        _queueMovementTimer = 0;
        _rbody = GetComponent<Rigidbody>();
    }

    private void FixedUpdate()
    {
        float xInput = Input.GetAxisRaw("Horizontal");
        float yInput = Input.GetAxisRaw("Vertical");
        yInput = xInput != 0 ? 0 : yInput;
        _directionalInput = new Vector3(xInput, yInput, 0);
        if (!IsMoving)
        {
            _rbody.velocity = Vector3.zero;
            if (_queueMovement != Vector3.zero)
            {
                Move(_queueMovement);
            }
            else
            {
                Move(_directionalInput);
            }
        }
        else
        {
            if (_rbody.velocity.magnitude < movementSpeed) _rbody.velocity = Vector3.zero;
            if (queueMovementCooldown > _queueMovementTimer)
            {
                _queueMovement = _directionalInput;
                _queueMovementTimer += Time.deltaTime;
            }
            else
            {
                _queueMovement = Vector3.zero;
            }
        }
    }

    private void Move(Vector3 direction)
    {
            _rbody.velocity = direction * movementSpeed;
    }

    private void OnDrawGizmos()
    {
        Gizmos.color = Color.green;
        Gizmos.DrawLine(transform.position, transform.position + _directionalInput);
        Gizmos.color = Color.yellow;
        Gizmos.DrawLine(transform.position, transform.position + _queueMovement);
    }
}

To get slick movement like this it is VERY hard to do it with physics.

One way is to bevel your collider, or even make it a sphere.

Another way is to not use physics and instead do all the collision yourself, but that is much more involved. Here is how I did it in Bomberman: Bomberman / Hallway / corner movement behavior:

With Bomberman it is CRITICAL that you never get “snagged” on a corner.

1 Like