Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Velocity.magnitude never "stop"

Discussion in 'Scripting' started by tet2brick, Jun 20, 2012.

  1. tet2brick

    tet2brick

    Joined:
    Jun 24, 2009
    Posts:
    77
    Hi,

    I use the SimpleMove function on a CharacterController, I wanted to use the magnitude of the rigidbody to set the move/iddle animation of the object.

    Code (csharp):
    1. function Update () {
    2.     Move (); //the function to set the object moving with simplemove
    3.     var horizontalVelocity = controller.velocity;
    4.         horizontalVelocity = Vector3(controller.velocity.x, 0, controller.velocity.z); 
    5.         Debug.Log(horizontalVelocity.magnitude);
    6.        SendMessage("SetSpeed", horizontalVelocity.magnitude, SendMessageOptions.DontRequireReceiver);//here i send the setspeed message to update the animation
    7. }
    But when the end point is reached, the magnitude of the rigidboy is not set to zero. I don't know if it's a normal behaviour, but I look for a way to detect that the rigidbody is not moving anymore.

    Thanks a lot :)
     
  2. Meltdown

    Meltdown

    Joined:
    Oct 13, 2010
    Posts:
    5,816
    Just check it's < 0.1 or some other threshold.
     
    C_R_M likes this.
  3. angrypenguin

    angrypenguin

    Joined:
    Dec 29, 2011
    Posts:
    15,614
    Yeah, it's "standard" insofar as that's the nature of floating point numbers in computers. They're not exact, they're close approximations of values. So after a bunch of mathematical operations, instead of being exactly the figure you expect, they might be 0.0000000003 off or something along those lines. (It's why in the Editor the Transform component often shows things like 3.98324E-19 instead of 0, or 9.999999997 instead of 10, and so on.)

    The inaccuracy is tiny, but it's there, and it's enough to stop an == operator from returning true, and that's true of any implementation of floating point numbers on any computer system. As such, when using floats, it's best practise to check for less/greater than instead of quality. (That's also actually true for discreet number types, but for a completely different reason.) So pick some acceptable threshold, and check that your value is within that threshold of your target.

    There's also Mathf.approximate, but you still shouldn't use that for decision switching where avoidable.
     
    C_R_M likes this.
  4. tet2brick

    tet2brick

    Joined:
    Jun 24, 2009
    Posts:
    77
    Hi,

    Thanks for your answers, I was already checking for less/greater, not on an absolute equality, the problem was the magnitude was always at 6 or seven, even with the model not moving.
    But i set a characterController.simplemove(0,0,0) and it do the trick.

    Thanks anyway for the info about floating point numbers in computers, I didn't know that :)