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 Thanks in advance!
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
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): function FrameObject(cam: Camera, target: GameObject) { // D = R / sin( FOV/2 ); if(target) { var c: MeshCollider = target.GetComponent("MeshCollider"); if(c) { var r: float = (c.bounds.max - c.bounds.center).magnitude; var fov: float = cam.fieldOfView; var d: float = r / Mathf.Sin( Mathf.Deg2Rad * (fov*0.5) ); distance = d + cam.nearClipPlane; } } }
You may also want to use Transform.LookAt() to align your camera appropriately. http://unity3d.com/support/documentation/ScriptReference/Transform.LookAt.html