Unknown Inconsistency

When I run my program this script below is to control movement and collision on the x axis, however, for some unknown reason the player stops further from walls on one side compared to the other (shown in attached images). Would anyone know how to make it so the player stops a consistent distance from walls on both sides? Also I’m using unity’s tilemap system for the map.

public class PlayerMovement : MonoBehaviour
{
    Vector3 moveDelta;
    [SerializeField]
    float movementSpeed;
    [SerializeField]
    float jumpHeight;
    [SerializeField]
    GameObject player;
    RaycastHit2D hit;

    void Update()
    {
        if (Input.GetKeyDown(KeyCode.Space) && player.GetComponent<Gravity>().ground.collider != null)
        {
            transform.position += new Vector3(0, 0.2f, 0);
            player.GetComponent<Gravity>().falling = -jumpHeight;

        }
    }
    private void FixedUpdate()
    {
        float x = Input.GetAxisRaw("Horizontal");

        moveDelta = new Vector3(x * Time.deltaTime * movementSpeed, 0, 0);

        hit = Physics2D.BoxCast(transform.position + new Vector3(0,0.1f,0), new Vector2(1f, 1.8f), 0, new Vector2(moveDelta.x, 0), Mathf.Abs(moveDelta.x), LayerMask.GetMask("Ground"));
        Debug.Log(moveDelta);
        if (hit.collider == null)
        {
            transform.Translate(moveDelta);        
        }
    
    }
}

7980132--1024494--On left wall.png

7980132--1024491--On right wall.png

TIA :slight_smile:

7980132--1024494--On left wall.png

Probably related to your sprite not being centered or your tilemap tile colliders not being formed properly to the image.

It’s possible, but the sprite is just unity’s default square with a y scale of 2 and it happens even when not using tilemaps

Other things it could be:

  • Your camera might be slightly rotated and there’s a z-axis position difference
  • You might be using a perspective camera and there’s a z-axis position difference
  • you might not be using a pixel-perfect camera which can sometimes cause gaps with sprites