Search Unity

[Fixed] Grid Based Collision Problem

Discussion in '2D' started by SkyTech6, Apr 19, 2017.

  1. SkyTech6

    SkyTech6

    Joined:
    Jul 14, 2015
    Posts:
    151
    Hey, so I'm doing some 2D grid based movement and my collision script is having a problem.

    Here's the script:
    Code (CSharp):
    1. public class Movement : MonoBehaviour {
    2.  
    3.     public float speed = 2.0f;
    4.     Vector3 pos;
    5.     Vector3 old;
    6.     Transform tr;
    7.  
    8.     void Start()
    9.     {
    10.         pos = transform.position;
    11.         tr = transform;
    12.     }
    13.  
    14.     void FixedUpdate()
    15.     {
    16.         if (Input.GetKey(KeyCode.UpArrow) && tr.position == pos)
    17.         {
    18.             pos += (Vector3.up);
    19.         }
    20.         else if (Input.GetKey(KeyCode.RightArrow) && tr.position == pos)
    21.         {
    22.             pos += (Vector3.right);
    23.         }
    24.         else if (Input.GetKey(KeyCode.DownArrow) && tr.position == pos)
    25.         {
    26.             pos += (Vector3.down);
    27.         }
    28.         else if (Input.GetKey(KeyCode.LeftArrow) && tr.position == pos)
    29.         {
    30.             pos += (Vector3.left);
    31.         }
    32.  
    33.         transform.position = Vector3.MoveTowards(transform.position, pos, Time.deltaTime * speed);
    34.     }
    35.  
    36.     void OnCollisionEnter2D()
    37.     {
    38.         Debug.Log ("Contact");
    39.  
    40.         float x = transform.position.x;
    41.         float y = transform.position.y;
    42.         float z = transform.position.z;
    43.  
    44.         if ((int)(x + 0.5) > (int)x && x > 0)
    45.             x = (int)x + 0.5f;
    46.         else if((int)(x - 0.5) < (int)x && x < 0)
    47.             x = (int)x - 0.5f;
    48.         else
    49.             x = (int)x;
    50.  
    51.         if ((int)(y + 0.5) > (int)y && y > 0)
    52.             y = (int)y + 0.5f;
    53.         else if ((int)(y - 0.5) < (int)y && y < 0)
    54.             y = (int)y - 0.5f;
    55.         else
    56.             y = (int)y;
    57.  
    58.         z = 0f;
    59.  
    60.         Vector3 away = new Vector3(x, y, z);
    61.         pos = away;
    62.     }
    63. }
    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:
    http://i.imgur.com/YNmpHsf.gifv

    Any thoughts on what is wrong in the code?
     
    Last edited: Apr 19, 2017
  2. Zephus

    Zephus

    Joined:
    May 25, 2015
    Posts:
    356
    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?
     
  3. SkyTech6

    SkyTech6

    Joined:
    Jul 14, 2015
    Posts:
    151
    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)
     
    Last edited: Apr 19, 2017
  4. SkyTech6

    SkyTech6

    Joined:
    Jul 14, 2015
    Posts:
    151
    Found the fix and posting it below for anyone that comes across this problem.


    Code (CSharp):
    1.     void OnCollisionEnter2D()
    2.     {
    3.         Debug.Log ("Contact");
    4.  
    5.         float x = transform.position.x;
    6.         float y = transform.position.y;
    7.         float z = transform.position.z;
    8.         Debug.Log ("First numbers: " + x + "," + y);
    9.  
    10.         if ((int)(x + 0.5f) > (int)x && x > 0) { //Obstacles to the Right
    11.             x = (int)x + 0.5f;
    12.             Debug.Log ("1st If");
    13.         } else if ((int)(x - 0.5f) < (int)x && x > 0) { //Obstacles to the Left
    14.             x = (int)x + 0.5f;
    15.             Debug.Log ("2st If");
    16.         }
    17.         else
    18.             x = (int)x;
    19.  
    20.         if ((int)(y + 0.5f) > (int)y && y > 0) { //Obstacles Above
    21.             y = (int)y + 0.5f;
    22.             Debug.Log ("3st If");
    23.         } else if ((int)(y - 0.5f) < (int)y && y > 0) { //Obstacles Below
    24.             y = (int)y + 0.5f;
    25.             Debug.Log ("4st If");
    26.         }
    27.         else
    28.             y = (int)y;
    29.  
    30.         z = 0f;
    31.  
    32.         Vector3 away = new Vector3(x, y, z);
    33.         pos = away;
    34.     }
    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