Object following the player,Object points to player upon entering a certain range

Hi All,

I’m trying to create a scene in which my object is a cctv that rotates around.
Once it detects that the player is in it’s detection zone, it will point at the player.
I’m lost in how I should tackle this problem. I have an animation for the cctv but which function should i add to allow it have a detection zone? Also to stop the animation should I use the trigger function?
,Hi all,

One of my object is a cctv and it is currently turning 80 to -80 degrees with an animation.
I’m currently stuck at the part where I want this cctv to point at a player when the player moves towards the certain range that the cctv can detect.
Is that using another animation or a script trigger function?
Thanks so much!

You could use Vector3.Distance.

Very basic script wich is may unoptimized.

using UnityEngine;

public class LookAt : MonoBehaviour
{
public Transform target;

public float minimumDistance;

private void Update()
{
    if (Vector3.Distance(transform.position, target.position) < minimumDistance)
    {
        // Draw line from, to
        Debug.DrawLine(transform.position, target.position, Color.green);
        transform.LookAt(target);
    }
    else
    {
        //reset rotation. You could store the old rotation and smoothdamp
        transform.rotation = new Quaternion(0, 0, 0, 0);
    }
}

}