Smootlookat modified script

I am new to Unity, and very new to scripting. I am trying to make a script where the camera looks at a gameobject when you get close enough to it

var target : Transform;
var damping = 6.0;
var smooth = true;
var range = 6.0;

@script AddComponentMenu(“Camera-Control/Smooth Look At”)

function LateUpdate () {
if (target) {
if (smooth)
{
// Look at and dampen the rotation
var rotation = Quaternion.LookRotation(target.position - transform.position);
transform.rotation = Quaternion.Slerp(transform.rotation, rotation, Time.deltaTime * damping);
}
else
{
// Just lookat
transform.LookAt(target);
}
}
}

function Start () {
// Make the rigid body not change rotation
if (rigidbody)
rigidbody.freezeRotation = true;
}

I’m pretty sure I failed badly at it

Hi Cbjfan1,

If you want to make the camera look at the gameobject (Target) when it is close enough, then you could check the distance of the game object from the camera by using something like:

function LateUpdate () 
{ 
    if(transform.InverseTransformPoint(target.position).magnitude <= desiredDistance)
    {
       if (target) 
       { 
         if (smooth) 
         {
       // Look at and dampen the rotation 
            var rotation = Quaternion.LookRotation(target.position - transform.position); 
            transform.rotation = Quaternion.Slerp(transform.rotation, rotation, Time.deltaTime * damping);
         } else 
         { // Just lookat 
            transform.LookAt(target); 
         }
      }
   }
}
        
function Start () { // Make the rigid body not change rotation if (rigidbody) rigidbody.freezeRotation = true; }

Inside this if, you could put the entire LateUpdate content, and it should get you the results you are requesting.

Hope this help.