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. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Vector2.Distance Calculated Inconsistently Issue

Discussion in 'Scripting' started by Kalita2127, Nov 14, 2016.

  1. Kalita2127

    Kalita2127

    Joined:
    Dec 6, 2014
    Posts:
    265
    I was trying to calculate the distance between the stacked object and the instantiated object. And then I found the issue that the Vector2.Distance not calculated consistently. Here is what it looks like :


    And the prefab is like in this picture :

    https://snag.gy/khHcvT.jpg

    This issue is seriously makes me crazy that I almost gave up on my project. :( Can someone please explain why is this happened ? Thank you.
     
    Last edited: Nov 14, 2016
  2. edwin_s

    edwin_s

    Joined:
    Mar 10, 2015
    Posts:
    19
    It's not very clear what you're trying to do, or how you're doing it. Maybe you can elaborate and/or post some code?
     
  3. LeftyRighty

    LeftyRighty

    Joined:
    Nov 2, 2012
    Posts:
    5,148
    if you're talking about the really small number in one of those inspector fields or the very small differences, I'd recommend looking up "floating point (in)accuracy"
     
  4. Kalita2127

    Kalita2127

    Joined:
    Dec 6, 2014
    Posts:
    265
    So what I was doing is I'm calculating the distance of the instantiated object and the object at the top of the stack using Vector2.Distance. The code involved to this issue is just Vector2.Distance(instantiatedObject, topStack), nothing fancy. And what I was asking is why is the method Vector2.Distance is calculating inconsistently whenever I do the distance calculation as in my screen record above. You can see that the value is changed inconsistently in console and I instantiated it on the same point.

    Hope you guys understand what I explained.
     
    Last edited: Nov 14, 2016
  5. edwin_s

    edwin_s

    Joined:
    Mar 10, 2015
    Posts:
    19
    Your stack is moving though. Also, float values aren't accurate by nature, as @LeftyRighty pointed out.
     
  6. Kalita2127

    Kalita2127

    Joined:
    Dec 6, 2014
    Posts:
    265
    It should be no problem even if the stack is moving because I put the collider trigger at the bottom of the prefab so it will capture the distance whenever the collider trigger touch the top of the stack.
    https://snag.gy/7EWYIb.jpg
    https://snag.gy/QrlEIx.jpg

    In my case the different is about 0.03 to 0.1. I don't think the float value is very inaccurate. The float value is inaccurate indeed but the different should be very-very tiny.
     
    Last edited: Nov 14, 2016
  7. edwin_s

    edwin_s

    Joined:
    Mar 10, 2015
    Posts:
    19
    So you do this calculation when the new box falls on top of the stack, when the collider at the bottom of your box hits the stack? I think collision detection is fairly lax by default (to save resources) and it may not always detect the collision in the exact same moment. Do you get the same results with continuous collision detection?
     
  8. Kalita2127

    Kalita2127

    Joined:
    Dec 6, 2014
    Posts:
    265
    Yes it also not consistent if I tried it with OnTriggerStay or with the Update function.
     
  9. edwin_s

    edwin_s

    Joined:
    Mar 10, 2015
    Posts:
    19
    You could Debug.Log the position of both the top of the stack as well as the landing box, and compare those values. I highly doubt that Vector2.Distance is giving you the wrong value, so something must be off in the positioning. That, or even continuous collision detection isn't accurate enough, but I doubt it should make 0.3 difference, that seems too much.
     
    Last edited: Nov 14, 2016
  10. Kalita2127

    Kalita2127

    Joined:
    Dec 6, 2014
    Posts:
    265
    I'm sorry I can't understand what you mean ?

    I said that it's giving me an inconsistent value.

    This guy in 2013 also have the same problem with Vector2.Distance and it seems he still don't have the answer of this issue. I already did some googling and I found only 1 result with this issue.
     
  11. edwin_s

    edwin_s

    Joined:
    Mar 10, 2015
    Posts:
    19
    On collision, use Debug.Log to put the position of the box and the stack in the console, so you can calculate the distance yourself and/or see why it's different every time.


    Indeed, and I am trying to help you find the cause.
     
  12. Kalita2127

    Kalita2127

    Joined:
    Dec 6, 2014
    Posts:
    265
    Sorry for the wait sir. So I already tested it again with manual calculation and the result is they are still inconsistent.
    This is the piece of code that I used for testing :
    Code (CSharp):
    1. float distance = other.gameObject.tag == "Gedung" ?
    2.                 Vector2.Distance(transform.localPosition, other.transform.localPosition)
    3.                 :
    4.                 Vector2.Distance(transform.localPosition, DistComparer.Instance.gameObject.transform.localPosition); // > 1.21f && < 1.23f
    5.             Debug.Log("This pos is : "+transform.localPosition);
    6.             Debug.Log("Other pos is : "+other.transform.localPosition);
    7.             Vector2 temp = transform.localPosition - other.transform.localPosition;
    8.             Debug.Log("Distance is : "+distance);
    9.             Debug.Log("MyDistance is : "+Mathf.Sqrt(Mathf.Pow(temp.x, 2) + Mathf.Pow(temp.y, 2)));
    The reason why I'm using localPosition is because all the instantiated object will be parented to the object in hierarchy.
    https://snag.gy/AqRelz.jpg
    Thanks :). I really appreciate it !
     
  13. Suddoha

    Suddoha

    Joined:
    Nov 9, 2013
    Posts:
    2,824
    Vector2.Distance is accurate.

    It's the (2D) Physics System which causes the inaccurate values. The colliders won't touch each other as accurate as you'd expect. Neither when dropping them onto each other, nor when placing them accurately onto each other.

    The one dropping onto another probably moves until collision is recognized. At this point you're grabbing the positional data which is not accurate as it's gone a little too far.
    You may think you can grab it one Update or FixedUpdate later, but then it'll be a little off too as it got pushed out again.
     
  14. edwin_s

    edwin_s

    Joined:
    Mar 10, 2015
    Posts:
    19
    If you are calculating the distance between two objects, it probably won't make a difference if you do that between their local positions or their world positions (so long as they have the same parent).


    I'm glad to hear it from someone else as well. This does indeed make the most sense.
     
  15. Kalita2127

    Kalita2127

    Joined:
    Dec 6, 2014
    Posts:
    265
    I see, that makes sense about what's the cause of inaccurate value. Thank you sir !
    But now in my case, what trick should I do to get the accurate distance ? I still need rigidbody and I need gravity for my prefab.
     
  16. Suddoha

    Suddoha

    Joined:
    Nov 9, 2013
    Posts:
    2,824
    I've read that PolygonCollider2D might help, but I'm afraid it won't satisfy your needs either.

    I've seen your video, but that's probably not all that you need the distance for? Or are there always boxes which could perfectly align?
     
  17. Kalita2127

    Kalita2127

    Joined:
    Dec 6, 2014
    Posts:
    265
    What will PolygonCollider2D do to if I use it ? Actually I used distance to indicate whether the instantiated object :
    1. Will stick if the distance is near the top of the stack
    2. Will vulnerable to collapse at the certain value
    3. Will ignore the collision and fall if the distance is far from the top of the stack
     
  18. Suddoha

    Suddoha

    Joined:
    Nov 9, 2013
    Posts:
    2,824
    Well yeah, but wouldn't there be a different way to calculate the values that you need? I mean, do you think that you can only solve it with Physics being involved?
     
  19. Kiwasi

    Kiwasi

    Joined:
    Dec 5, 2013
    Posts:
    16,860
    This can all be done within the bounds of the accuracy shown in your numbers.
     
  20. Kalita2127

    Kalita2127

    Joined:
    Dec 6, 2014
    Posts:
    265
    Yes, I think I can only solve it with physics. I don't have any other way to solve it other than using physics. Or maybe you have different approach to this problem ?

    What is that ? Can you explain it more detailed please ?