- Home /
The question is answered, right answer was accepted
Velocity working really weird
So i'm making basic platformer game in unity2D and i encountered really weird thing in unity 2020.2.2f1. I don't know if it's bug or just weird rules of unity but when object with rigidbody2D and collider2D is not touching anything , everything stays normal. The moment it touches ground, all velocities of falling object start to act like they're about to die. They change from for example -13f to hundreds of billions. I thought that maybe my script that changed velocity on x axis caused it but i made a simple test that looks like this:
void Update()
{
float yVel = rb.velocity.y;
float xVel = rb.velocity.x;
print("yVel = " + yVel + " " + "xVel = " + xVel);
}
And the results are this:
So far so good i haven't got any issues ( visually object stays in one place) related to this but i have huge concerns about future of my project because let's say i would like to change some velocities someday and i honestly don't know what will happen.
Could someone explain to me why is that happening, how can i prevent it or should i just leave it and don't care about that?
Answer by Eno-Khaon · Feb 07, 2021 at 08:19 PM
You seem to be misunderstanding scientific notation.
In short, you're seeing numbers displayed such as:
7.450581E-9
same as...
7.450581 * 10^-9
same as...
7.450581 * 0.000000001
same as...
0.000000007450581
The values are so comically small as to practically be zero, while still not being LITERALLY zero.
You won't see anything visibly move at that speed. However, it's important to note that a value that is *close* to zero isn't zero. If you need to watch for when a Rigidbody isn't moving, this is a good place for Mathf.Approximately().
// effectively: if(sqrMagnitude < Mathf.Epsilon) -- 0.00001f
if(Mathf.Approximately(rb.velocity.sqrMagnitude, 0f))
{
// Not moving
}
Thanks a lot, sorry for my mistake with scientific notation, value is changing many times per seconds and i lost that "-" somewhere :).
thank you for your time and answer!
Answer by RodrigoAbreu · Feb 07, 2021 at 08:31 PM
@Rain478293, Just a note - Everything physics based instead of using the Update, use the FixedUpdate. If you use it in Update you might experience some strange behaviours (precision). I'm not sure how you're using it, and also I'm not sure if you put it in the Update just for printing the values.
If those values are not causing any kind of problems in your game, then it's not a problem, they are approximations only.
Well it's literally chunk of code straight from the script so i didn't changed that. I'm little bit confused because last time some guy told me not to use fixedUpdate when coding movement.
Thanks a lot for the answer, have a nice day :)
$$anonymous$$ovement != Physics.
You can achieve movement without using physics.