Search Unity

camera to object distance

Discussion in 'Scripting' started by bhaal, Oct 22, 2009.

  1. bhaal

    bhaal

    Joined:
    Apr 18, 2009
    Posts:
    35
    Hi

    How to detect how for is the camera from object?

    I woudl like to make the object transparent if the camera will get close to in and show it back when the camera wil be further away.

    Cheers,
    Lucas
     
  2. caitlyn

    caitlyn

    Joined:
    Jun 20, 2008
    Posts:
    402
  3. bhaal

    bhaal

    Joined:
    Apr 18, 2009
    Posts:
    35
    Sorry for my noob`ish question but could You better explain it to me how to combine this code with my camera/object (in this case a poly object from Maya - pCube1)?
    I would be very gratful

    i tried whole day to make it but no luck.
    my knowledge in programming is... very poor :)
    I am an 3d artis :)

    cheers
     
  4. andeeeee

    andeeeee

    Joined:
    Jul 19, 2005
    Posts:
    8,768
    There's a nice trick you can use here. Get the object's heading vector:-
    Code (csharp):
    1. var heading: Vector3 = target.position - camera.transform.position;
    ...and then get the dot product of this with the camera's forward vector:-
    Code (csharp):
    1. var distance: float = Vector3.Dot(heading, camera.transform.forward;
    The advantage is that this gives you the distance inward from the plane of the "screen", and it is also a bit more efficient in a frame update.
     
  5. cfloutier

    cfloutier

    Joined:
    Jul 30, 2009
    Posts:
    35
    excellent, accurate, wonderfull. Thanks again andeee you saved my life once again