Search Unity

Question Vector3.distance, keep the same distance even if one gameobject bigger than the other

Discussion in 'General Discussion' started by Razputin, Oct 19, 2022.

  1. Razputin

    Razputin

    Joined:
    Mar 31, 2013
    Posts:
    356
    So I want to keep the float output from vector3.distance the same, even if one gameobject is bigger than the other. Currently if one object is bigger than the other the distance will increase slightly to account for the difference on the Y Axis. I cannot exclude the Y axis from the calculation as sometimes gameobjects are actually higher/lower than one another.

    What would be a good method to achieve this?

    My current thoughts.

    Determine distance based on the bottom of the objects collider.
    Move the objects pivot to it's bottom.

    Any other thoughts on how to achieve this before I attempt to implement one of those?

    Thanks!
     
  2. CodeSmile

    CodeSmile

    Joined:
    Apr 10, 2014
    Posts:
    5,970
    Add a child GameObject "anchor" to the objects you're measuring, move that "anchor" to the location where you want the measurement to be taken (ie bottom) and only use those "anchor" transforms to calculate distances.
     
  3. Razputin

    Razputin

    Joined:
    Mar 31, 2013
    Posts:
    356
    Yeah this might be easiest, thanks!