Search Unity

Camera Frame selected - how to

Discussion in 'Scripting' started by NYGhost, Nov 16, 2007.

  1. NYGhost

    NYGhost

    Joined:
    Sep 20, 2007
    Posts:
    55
    Hi All
    I'm trying to mimic Unity's menu command Edit->Frame Selected so I can focus the camera on the selected object.

    Any pointers on how to accomplish that are very much appreciated :D
    Thanks in advance!
     
  2. davebuchhofer

    davebuchhofer

    Joined:
    Nov 9, 2007
    Posts:
    126
    couldn't tell you how to do it in unity yet, but the basics are

    the distance from the camera D so that:

    D = R / sin( FOV/2 );

    where R is the object's bounding sphere radius
     
  3. NYGhost

    NYGhost

    Joined:
    Sep 20, 2007
    Posts:
    55
    Thanks that's what I needed
    here's a little snippet on how you could doit on Unity
    I used a mesh collider to aproximate the object's radius.

    Code (csharp):
    1.  
    2. function FrameObject(cam: Camera, target: GameObject) {
    3.     // D = R / sin( FOV/2 );
    4.     if(target) {
    5.         var c: MeshCollider = target.GetComponent("MeshCollider");
    6.         if(c) {
    7.             var r: float = (c.bounds.max - c.bounds.center).magnitude;
    8.             var fov: float = cam.fieldOfView;
    9.             var d: float = r / Mathf.Sin( Mathf.Deg2Rad * (fov*0.5) );
    10.             distance = d + cam.nearClipPlane;
    11. }
    12.     }
    13. }
    14.  
     
  4. CoherentInk

    CoherentInk

    Joined:
    Jul 16, 2006
    Posts:
    216