- Home /
Problem reading/getting from rb.velocity value?
Hi everyone, I am trying to give a particular object a boost of force on Y, when its own velocity if over a certain Y velocity.
So imagine a Mario 1 style game, if the mushroom was moving over the block below, and the block below nudged it upwards this will pause it to increase its velocity.y naturally. In my code I'm trying to add a significant boost to it when that happens.
Its very confusing, because I can see by my Debug.Log messages that the velocity is over the set amount, but the boost isnt occuring (NOTE: If i make the set amount for boosting very slow such as 1f or less, then every time it goes over an edge of platform it gets the boost (presumably because it gets a slight bump at the end of the platform). Here is the code, and then the Debug messages, which I think when looked at together describe the problem perfectly.
Can anybody tell me how its possible that the Debug reports the velocity.y to be 5.96... , but the then code that says if (vel.y > 5.9f) // Do this , never gets called?
(NOTE: I already tried putting this part of the code into LateUpdate() and FixedUpdate() but it seemed to have no effect)
if (rb.velocity.y > 0 )
Debug.Log("vel y = " + rb.velocity.y);
if (rb.velocity.y > 5.5f)
{
Debug.Log("why doesnt it boost!" + rb.velocity.y);
if (!beingBoosted)
{
Debug.Log("BOOSTING NOW = " + rb.velocity.y);
rb.AddForce(0f, 0.7f, 0, ForceMode.Impulse); // add small boost to make powerup jump
beingBoosted = true;
}
}
the log reads:
vel y = 5.960464E-08 UnityEngine.Debug:Log(Object) PowerupBig:LateUpdate() (at Assets/Scripts/PowerupBig.cs:75)
vel y = 1.266599E-07 UnityEngine.Debug:Log(Object) PowerupBig:LateUpdate() (at Assets/Scripts/PowerupBig.cs:75)
vel y = 5.960464E-08 UnityEngine.Debug:Log(Object) PowerupBig:LateUpdate() (at Assets/Scripts/PowerupBig.cs:75)
vel y = 1.655453 UnityEngine.Debug:Log(Object) PowerupBig:LateUpdate() (at Assets/Scripts/PowerupBig.cs:75)
Answer by pako · Dec 15, 2017 at 07:59 PM
@megabrobro you are not reading the log right. It doesn't say velocity.y is 5.96, it says velocity.y is 5.960464E-08, which is practically zero! i.e. 5.960464E-08 = 5.960464 x 10^(-08) (read 5.96 times 10 to the power of -8, which is the same as 5.96 divided by 100,000,000 => practically zero). So, this is why vel.y > 5.9f is false.
You can use rb.velocity.y.ToString("0.00")
to display the float value to 2 decimal places.