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

Question My project prints RigidBody2d.velocity.y = 3.814697e-06, but other project prints 0.

Discussion in 'Scripting' started by WayneJP, Sep 16, 2022.

  1. WayneJP

    WayneJP

    Joined:
    Jun 28, 2019
    Posts:
    44
    I know 3.814697e-06 equates to 0.000003814697, and I know I can use
    (float)System.Math.Round(value,2)
    to fix it.

    I don't know why two project print different result. I'm sure neither projects use Math.Round and scripts are same.
    I print velocity during my hero jump and fall, it print 3.814697e-06 when hero touch ground and keep move horizontally.
    Is there something I'm missing? Maybe somewhere I can change setting in unity editor?

    EDIT:
    • I found rb2d.velocity.y switch between 3.814697e-06 and 0 when hero move on the plane.
    • I notice that though I set rgbd2d.velocity = new vector(8f, 0f) in fixedUpdate, but it prints (8.00001 , 0) in update.
    • When I set rgbd2d's gravity scale to 0, the problem does not occur, and I get nice float value.
    • If I set Geometry Type in Composite Collider 2D in Tilemap to Polygons, and the problem does not occur too.
    • Found a similar thread, here is the link:https://forum.unity.com/threads/2d-...s-sometimes-frequently-video-attached.769373/
     
    Last edited: Sep 17, 2022
  2. RadRedPanda

    RadRedPanda

    Joined:
    May 9, 2018
    Posts:
    1,593
    Does that affect you at all? If you're comparing two floats with == you're doing it wrong anyways. You should be using Mathf.Approximately if you want to compare floats. Otherwise, there's no point in fixing it.
     
    Chubzdoomer likes this.
  3. WayneJP

    WayneJP

    Joined:
    Jun 28, 2019
    Posts:
    44
    Thanks for replying. I have updated question.
    I know it's wrong to compare two floats with ==. I just feel strange that rb2d.velocity.y is 3.814697e-06 instead zero but it supposed to be zero.
     
  4. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,713
    Essentially it is zero....
    3.814697e-06
    is called scientific notation.

    https://en.wikipedia.org/wiki/Scientific_notation

    Specifically it is "E Notation"

    Your number is 0.000003814697, which is a VERY tiny number.

    Screen Shot 2022-09-16 at 9.50.26 AM.png
     
  5. WayneJP

    WayneJP

    Joined:
    Jun 28, 2019
    Posts:
    44
    yes, you're right. I know it's scientific notation.
    It's just weird that I can't get a nice zero while other tutorial project I am learning can.
    Also I notice that though I set rgbd2d.velocity = new vector(8f, 0f) in fixedUpdate, but it prints (8.00001 , 0) in update.
    And if I set rgbd2d's gravity scale to 0, the problem does not occur, I get nice float value.
    If I set Geometry Type in Composite Collider 2D in Tilemap to Polygons, and the problem does not occur too.
     
    Last edited: Sep 17, 2022
  6. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    10,512
    I think the missing piece here is something you might not want to understand but in-case you do, you need to read-up about how floating point is implemented and how it doesn't have infinite precision. It can/does produce different results on different CPU/devices/platforms (choose your name) which is why software that uses floating-point cannot be 100% deterministic even if it's close enough in most cases.

    The reason why you don't compare floating-point values is the reason why you cannot expect an exact value everywhere on every CPU/device/platform. Often you are not aware of this but it becomes very apparent when the result of one calculation is fed back into the calculation for the next result. A small change results in a potential larger change next time. Physics algorithms do this but so do other systems, including (potentially) your own scripts.

    Just type "floating point precision" or "floating point arithmetic" into Google; it'll give you all you need to know.

    So don't fixate on this, it's irrelevant and something you should always expect. Always compare values using an approximate comparison like Mathf.Approximate as mentioned above. Even physics uses such a thing to decide if a Rigidbody2D is not moving and can go to sleep: Physics2D.linearSleepTolerance.

    Also, a Rigidbody2D moving along a horizontal surface doesn't have exactly zero vertical velocity. It'll go up/down slightly (very slightly) as gravity pushes it into the surface and the solver moves it out of the surface. It's a balancing act. Technically this is what you do if you were to slide along a horizontal surface, you're always slightly going into the surface and being pushed out of it; this results in friction. :)
     
    Anthiese and Kurt-Dekker like this.
  7. WayneJP

    WayneJP

    Joined:
    Jun 28, 2019
    Posts:
    44
    Hi, MelvMay. Thanks for replying!
    Can't agree more! I know I should not fixate on this and I won't. :)
    Allow me to ask one last question:
    Can I set Composite Collider 2D's Geometry Type to Polygons to deal with this problem?
    Well, I also curious why set it to polygons can make rigidbody2D have zero vertical or horizontal velocity.;)
     
  8. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    10,512
    This isn't a "problem", there's no need to fix it. It's nothing whatsoever to do with colliders.

    You've found a way to give you zero using a specific option on your device so you're assuming it'll always be zero when you use that specific option on any device and it doesn't work like that. Again, ignore it.
     
    Kurt-Dekker likes this.
  9. WayneJP

    WayneJP

    Joined:
    Jun 28, 2019
    Posts:
    44
    Ok, got it. Thank you!
     
    MelvMay likes this.