I’m trying to make the camera focus/frame on a clicked object, then when another object is clicked the camera should move and frame that object instead. How do I make the clicked objects targets for the camera? I attempted lookat but without success.
This is a slightly modified SmoothLookAt code. It focuses on various objects when I click on them. What I would like is to calculate their bounds so the camera zooms in/out in order to frame the object within the screen. Also if possible, I’d like to set the camera view so it looks at the objects largest bounds… ie if the object is a plane, the camera would look at it front on, not onto it’s edge. Is this at all possible?
Thanks.
#pragma strict
var target : Transform;
var damping : float = 6.0;
var smooth : boolean = true;
private var lookingAtTarget : boolean = false;
@AddComponentMenu("Camera-Control/Smooth Look At")
partial class SmoothLookAt { }
function CameraLookAt(t:GameObject) {
target = t.transform;
lookingAtTarget = false;
UpdateLookAt();
}
function UpdateLookAt () {
if (target !lookingAtTarget)
{
if (smooth)
{
while (!lookingAtTarget)
{
var relativePos = target.position - transform.position;
var rotation = Quaternion.LookRotation(relativePos);
transform.rotation = Quaternion.Slerp(transform.rotation, rotation, Time.deltaTime * damping);
var angle = Vector3.Angle(relativePos, transform.forward);
if (angle < 1.0) lookingAtTarget = true;
yield new WaitForFixedUpdate();
}
}
else
{
// Just lookat
transform.LookAt(target);
lookingAtTarget = true;
}
}
}