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

Possible bug with Vector3.Distance

Discussion in 'Scripting' started by CptHavvock, Aug 18, 2022.

  1. CptHavvock

    CptHavvock

    Joined:
    Dec 30, 2021
    Posts:
    12
    I've been trying to obtain the distance between a point and a mesh's vertex using this code
    Code (CSharp):
    1.  Debug.Log(Vector3.Distance(pDamage, transform.TransformPoint(mesh.vertices[i])));
    But everytime I execute it it says the same value (0.2598074), regardless of where the point is. I've checked to see if the vertices values are wrong but no, TransformPoint does give their exact position in the world space.

    So, if the point is always the same and the vertices are not in the same position, how is it that it always says the same value?
     
  2. RadRedPanda

    RadRedPanda

    Joined:
    May 9, 2018
    Posts:
    1,596
    Extremely unlikely that the Distance method is the problem. Hard to tell what the actual problem is without any other code. Did you verify that the point is always the same? Have you tried printing both the values out, and manually getting the distance between the two values?
     
  3. CptHavvock

    CptHavvock

    Joined:
    Dec 30, 2021
    Posts:
    12
    Yes, I changed the debug contents with both the vertices positions and the point and they both were correct (the point was always the one I put and the vertices were not all the same)
     
  4. CptHavvock

    CptHavvock

    Joined:
    Dec 30, 2021
    Posts:
    12
    The mesh im taking the vertices from is a cube, so the vertices I obtain are 8 groups 3 repeated positions, just like what a cube's mesh should be.

    The rest of the code was just me checking if the point given is 1 unit near at least one of the vertices, but for some reason vector3.Distance always gives me the same output.
     
  5. mgear

    mgear

    Joined:
    Aug 3, 2010
    Posts:
    9,019
    tried debug.drawline also to confirm positions?
     
  6. CptHavvock

    CptHavvock

    Joined:
    Dec 30, 2021
    Posts:
    12
    No, I'll try it out once I get home.
    Although what I don't understand is why vector3.Distance doesn't work when it should work normally when given two vector3.
     
  7. mgear

    mgear

    Joined:
    Aug 3, 2010
    Posts:
    9,019
    also is the transform.TransformPoint(..) using correct transform?

    you are not moving vertices in shader i guess?
    and its not skinnedmeshrenderer?

    could object scale affect anything?
     
  8. CptHavvock

    CptHavvock

    Joined:
    Dec 30, 2021
    Posts:
    12
    While I can't confirm anything, when I put just Debug.Log(transform.TransformPoint(mesh.vertices)) it gives me the 24 world positions of the vertices of the cube, meaning that this part should be working accordingly.
     
  9. CptHavvock

    CptHavvock

    Joined:
    Dec 30, 2021
    Posts:
    12
    Is it maybe because the script is inside the gameobject of the point? So transform.TransformPoint actually centers the vertices around the point, making the distance equal on all vertices?
     
    orionsyndrome likes this.
  10. Bunny83

    Bunny83

    Joined:
    Oct 18, 2010
    Posts:
    3,571
    Avoid just printing single values to the console, especially when you log mutliple values. Always write a proper log message so you can actually identify the message where it came from.

    The distance you read sounds like the distance from the corners of the cube to the center of the cube assuming the cube has a side length of 0.3 (so half the side would be 0.15). So if your "pDamage" position is the center of the cube, all vertices would have the same distance from the center.

    So to check this, as I said, use a proper log statement like
    Code (CSharp):
    1.  
    2. var p = transform.TransformPoint(mesh.vertices[i]);
    3. Debug.Log("#"+i + " dist from "+pDamage.ToString("0.000")
    4. " to " + p.ToString("0.000") + " is " + Vector3.Distance(pDamage, p), gameObject);
    Try this and copy the log messages here. You can manually verify the distances of each message since we print out the positions for each point. You can copy the messages either from the bottom half of the console window or from the editor log file.
     
    orionsyndrome likes this.