[Fixed] Grid Based Collision Problem

Hey, so I’m doing some 2D grid based movement and my collision script is having a problem.

Here’s the script:

public class Movement : MonoBehaviour {

    public float speed = 2.0f;
    Vector3 pos;
    Vector3 old;
    Transform tr;

    void Start()
    {
        pos = transform.position;
        tr = transform;
    }

    void FixedUpdate()
    {
        if (Input.GetKey(KeyCode.UpArrow) && tr.position == pos)
        {
            pos += (Vector3.up);
        }
        else if (Input.GetKey(KeyCode.RightArrow) && tr.position == pos)
        {
            pos += (Vector3.right);
        }
        else if (Input.GetKey(KeyCode.DownArrow) && tr.position == pos)
        {
            pos += (Vector3.down);
        }
        else if (Input.GetKey(KeyCode.LeftArrow) && tr.position == pos)
        {
            pos += (Vector3.left);
        }

        transform.position = Vector3.MoveTowards(transform.position, pos, Time.deltaTime * speed);
    }

    void OnCollisionEnter2D()
    {
        Debug.Log ("Contact");

        float x = transform.position.x;
        float y = transform.position.y;
        float z = transform.position.z;

        if ((int)(x + 0.5) > (int)x && x > 0)
            x = (int)x + 0.5f;
        else if((int)(x - 0.5) < (int)x && x < 0) 
            x = (int)x - 0.5f;
        else
            x = (int)x;

        if ((int)(y + 0.5) > (int)y && y > 0) 
            y = (int)y + 0.5f;
        else if ((int)(y - 0.5) < (int)y && y < 0) 
            y = (int)y - 0.5f;
        else
            y = (int)y;

        z = 0f;

        Vector3 away = new Vector3(x, y, z);
        pos = away;
    }
}

It seems that the problem stems from world positions. If I put my world all in the negatives of world space than the left and down collisions will work. However, if I put my world in the positives of world space than only up and right collisions work.

Gif of problem:

Any thoughts on what is wrong in the code?

Don’t have an answer to your question right now, but what are all those ‘if ((x + 0.5) > x)’ and the other 3 supposed to be for? x + 0.5 is always higher than x and x - 0.5 is always lower than x, isn’t it?

It figures out which direction you’re currently moving and which way to move you back.

If it helps…

When moving up - First and Third Ifs are called.
When moving right - First and Third Ifs are called.
When moving left - Only Third If is called. (Player gets stuck in wall)
When moving down - Only First If is called. (Player gets stuck in wall)

Found the fix and posting it below for anyone that comes across this problem.

    void OnCollisionEnter2D()
    {
        Debug.Log ("Contact");

        float x = transform.position.x;
        float y = transform.position.y;
        float z = transform.position.z;
        Debug.Log ("First numbers: " + x + "," + y);

        if ((int)(x + 0.5f) > (int)x && x > 0) { //Obstacles to the Right
            x = (int)x + 0.5f;
            Debug.Log ("1st If");
        } else if ((int)(x - 0.5f) < (int)x && x > 0) { //Obstacles to the Left
            x = (int)x + 0.5f;
            Debug.Log ("2st If");
        }
        else
            x = (int)x;

        if ((int)(y + 0.5f) > (int)y && y > 0) { //Obstacles Above
            y = (int)y + 0.5f;
            Debug.Log ("3st If");
        } else if ((int)(y - 0.5f) < (int)y && y > 0) { //Obstacles Below
            y = (int)y + 0.5f;
            Debug.Log ("4st If");
        }
        else
            y = (int)y;

        z = 0f;

        Vector3 away = new Vector3(x, y, z);
        pos = away;
    }

Once you asked what those If statements do, I started rubber ducking them and noticed that statements 2 and 4 would never be called that way. So fixed that and it all works now. Thanks XD