Search Unity

[Solved] Finding the distance of an object relative to a plane

Discussion in 'Scripting' started by Murgilod, Jul 15, 2017.

  1. Murgilod

    Murgilod

    Joined:
    Nov 12, 2013
    Posts:
    10,157
    So this is a bit of a weird one, but math was never my strong suit, so I need help here.

    I'm working on a focus pulling system for my cameras and I need to get the distance from a camera relative to the plane of the camera itself. I can probably explain better with a diagram.


    So here we have a camera with a single object in its view frustum. The green line represents the actual distance I need to be considering when setting the focus distance. The blue line represents the value I get from Vector3.distance. As you can probably tell, the blue line is actually longer than the green line. What I want to know is how to get the value of the green line when I know the position of the camera and the object transform I'm focusing on.

    Any help would be great, thanks.
     
    StrangeWays777 and Reahreic like this.
  2. Murgilod

    Murgilod

    Joined:
    Nov 12, 2013
    Posts:
    10,157
    Okay, I managed to figure it out on my own. It turns out Unity has a solution for this sort of thing practically built in.

    For anyone who stumbles across this thread in a google search or whatever:

    Code (CSharp):
    1. Plane plane = new Plane();
    2.             plane.SetNormalAndPosition(this.transform.forward, this.transform.position);
    3.  
    4.             Debug.Log(plane.GetDistanceToPoint(someExternalTransform.position));
    I just make a plane, set its normal as the forward vector of the camera and set the point on the plane to the camera position. Then I just use plane.GetDistanceToPoint to find out how far away the object is. It's all super simple stuff and I'm kinda kicking myself for not knowing it was there in the API.
     
  3. JoePatrick

    JoePatrick

    Joined:
    Nov 3, 2013
    Posts:
    121
    You could also use Vector3.Project, would be much cleaner

    The magnitude of this vector would be the distance
     
    Reahreic likes this.
  4. Murgilod

    Murgilod

    Joined:
    Nov 12, 2013
    Posts:
    10,157
    That was one of the first things I tried and, for the life of me, I was unable to get it to work the way I needed at all. Anyway, this is code that I can understand at a glance and gets the job done, so I suppose this will do for now.
     
  5. ippdev

    ippdev

    Joined:
    Feb 7, 2010
    Posts:
    3,853
    I like your solution.. worked great on the medical implant conformity app I am working on.
     
  6. jojoblaze

    jojoblaze

    Joined:
    Aug 2, 2010
    Posts:
    19
    Vector3.Project does not work with planes not in the origin.
     
  7. Ameerah2292

    Ameerah2292

    Joined:
    Dec 15, 2013
    Posts:
    1
    Hello.
    Thank you so much for sharing your solution to this issues. With your solution, I'm able to solve the issue for my project. :) I'm implementing this to calculate the distance of the target object and ARCamera.
    Thank you again.
     
  8. iamknew3

    iamknew3

    Joined:
    Jul 4, 2020
    Posts:
    16
    Hi, I had the same question and found this early post.

    Does this also work?
    Camera.main.WorldToScreenPoint(obj.transform.position).z


    From my testings, it seems to be identical compared to the plane method.