Search Unity

  1. We are migrating the Unity Forums to Unity Discussions by the end of July. Read our announcement for more information and let us know if you have any questions.
    Dismiss Notice
  2. Dismiss Notice

Question Vector2.up doesn't always equal (0.00, 1.00)?

Discussion in 'Editor & General Support' started by PlagenShiki, Oct 24, 2023.

  1. PlagenShiki


    Aug 4, 2022
    I'm running into a weird issue when trying to determine where the player is hitting on a hitbox.

    I am using this code to detect a collision and then determine if they hit the wall or the floor part of a block.
    Code (CSharp):
    1. private void OnCollisionEnter2D(Collision2D collision)
    2.     {
    3.         //First, check if it is the ground or not.
    4.         if(collision.gameObject.tag == "Ground")
    5.         {
    6.             Vector2 colVect = collision.contacts[0].normal;
    7.             Debug.Log(colVect);
    8.             //Now, check if it is the wall or the floor. So we check contacts.
    9.             if (colVect == Vector2.up)
    10.             {
    11.                 //We're on the floor! :D
    12.                 isGrounded = true;
    13.                 isJumping = false;
    14.                 Debug.Log("Floor!");
    15.             }
    16.             else if(colVect == Vector2.left || colVect == Vector2.right)
    17.             {
    18.                 //We're hitting a wall!
    19.                 touchingWall = true;
    20.                 Debug.Log("Wall!");
    21.             }
    22.             else
    23.             {
    24.                 Debug.Log("THINGS F***ED!");
    25.             }
    26.         }
    27.     }
    My problem is, that sometimes apparently Vector2.up isn't equal to 0.00, 1.00, even though it totally is?

    Using my debug output, I get the following instances:
    (0.00, 1.00)
    THINGS F***ED!
    (0.00, 1.00)
    (0.00, 1.00)
    (0.00, 1.00)
    THINGS F***ED!

    Where the (0.00, 1.00) is the Debug.Log(colVect) and the text is what part of the if/else it ends up in.

    Any idea why this is happening? (0.00, 1.00) should always be equal to Vector2.up, but sometimes it doesn't go into the if statement for some reason?

    You're asking for float equals which is tricky. Search for floating point imprecision for more information on the subject, it is a computer-wide thing, not Unity specific.
    What's more or less Unity specific (well, .NET) is that ToString (and by extension Debug.Log) will output a rounded floating point number for you.

    Your incoming vector may contain more digits. Try logging the components out with a more precise formatter.

    And it's always a bad idea to rely on floating point number equality. Avoid this as much as possible.
  3. CodeSmile


    Apr 10, 2014
    You can make a utility method like so:
    Code (CSharp):
    1. public static bool AreEqual(Vector2 a, Vector2 b) =>
    2.     Mathf.Approximately(a.x, b.x) && Mathf.Approximately(a.y, b.y);
    Hope it's correct, didn't test it.
  4. CodeSmile likes this.