Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Why my Rigidbody2D.velocity.y is not working correctly?

Discussion in 'Scripting' started by videoanime, Feb 22, 2015.

  1. videoanime

    videoanime

    Joined:
    Sep 28, 2014
    Posts:
    42
    Hi, I have this code

    Code (CSharp):
    1. void FixedUpdate ()
    2.         {
    3.         piso = Physics2D.OverlapCircle (gdchk.position, gdradius, 1 << LayerMask.NameToLayer("suelo")); //es un booleano que sera true o false si toca el suelo o no
    4.        
    5.         //if (piso || (Mathf.Floor(Mathf.Abs(rigidbody2D.velocity.y)) == 0 && transform.position.y < 7.1))
    6.         if (piso || (rigidbody2D.velocity.y == 0 && transform.position.y < 7.1))
    7.             {
    8.             Debug.Log (rigidbody2D.velocity.y);
    9.             //Debug.Log (Mathf.Floor(Mathf.Abs(rigidbody2D.velocity.y)));
    10.             //elemento.rigidbody2D.isKinematic = true;
    11.             suelo = true;
    12.             Semillero2.pieza = false;
    13.             }
    14.         //Debug.Log (suelo);
    15.         }
    And I get this 382 printings of zero and when my gameobject touches the bottom, there are several printings of different velocities below zero, I've watched that the first negative velocity is -1.962, so, why then it is zero since the if clause.

    My purpose with this code is to don't move the gameobjects that are falling down when these can't descend anymore.

    In other script I have the code to move them left and right and down faster. Here is my code

    Code (CSharp):
    1. void FixedUpdate ()
    2.         {
    3.         piso = Physics2D.OverlapCircle (gdchk.position, gdradius, 1 << LayerMask.NameToLayer ("suelo")); //es un booleano que sera true o false si toca el suelo o no
    4.  
    5.         if (!piso && rigidbody2D.velocity.y < 0)
    6.             {
    7.             //elemento.rigidbody2D.isKinematic = false;
    8.             float acelera = Input.GetAxis ("Vertical"); //se obtiene el movimiento vertical
    9.             if (acelera > 0)
    10.                 rigidbody2D.velocity = new Vector2 (rigidbody2D.velocity.x, 0);
    11.             else
    12.                 rigidbody2D.velocity = new Vector2 (rigidbody2D.velocity.x, acelera * MaxVel);
    13.  
    14.             float DerIzq = Input.GetAxis ("Horizontal"); //se obtiene el movimiento horizontal
    15.             rigidbody2D.velocity = new Vector2 (DerIzq * MaxVel, rigidbody2D.velocity.y);
    16.             }
    17.         }
    Here I don't have problem because the first variable is always false until I touch the bottom and the velocity in "y" is always negative. It's the same velocity in both scripts, why in one script is zero and in other is negative?

    Thanks.
     
  2. hpjohn

    hpjohn

    Joined:
    Aug 14, 2012
    Posts:
    2,190
    in the first code you have
    if(piso OR velocity ==0)
    if piso is TRUE, it wont even bother testing velocity, which is why it prints non-zero values
    in the second code you have
    if(NOT piso AND velocity is LESS THAN 0)
    so it can ONLY print negative values

    nb. it's generally considered bad to test for float value with == because 0.00000001 is essentially zero (nothing moves) but it's not == 0
     
  3. videoanime

    videoanime

    Joined:
    Sep 28, 2014
    Posts:
    42
    Ohhhhh, I see, that's why I used after this Mathf.Floor(Mathf.Abs(rigidbody2D.velocity.y) because with this I eliminate the sign and I could convert any decimal to zero, even so, the supposed velocity is all the time zero.

    Other thing, do you know how I can detect when the gameobject stops moving? You see, I tried with these velocities and it doesn't serve me.