# [Problem] "Security Camera" rotation bounds - stop rotation if player goes past a specific point

Background:
I am working on a Security Camera prefab. It is fairly simple so far. I have a camera model that I make look at the player if the player is within some specified distance to the camera. Otherwise it pans back and forth between two points I have set in the 3D space close to the camera.

Problem:
The problem I am having is I want the camera to never go past those limits. If the player moves past it I want the camera to stay looking at the limit until the player is out of range. I will post my full code.

Any help is appreciated, I have been trying to figure out the best way to do this for a while now.

Code:

/* Settings */
var range = 40;
var followSpeed : float = 1.0;
var panSpeed : float = .5 ;

/* Exposed Objects */
var player : Transform;
var leftLimit : Transform;
var rightLimit : Transform;

private var currentPanTarget : Transform = rightLimit;

function Start(){
// pick a random Direction
if(Random.Range(1, 10) % 2 == 0){
currentPanTarget = leftLimit;
} else {
currentPanTarget = rightLimit;
}
}

function Update () {
// calculate distance to player
var currentDistance = Vector3.Distance(transform.position, player.position);

/* if the player is within the range panTo player */
if(currentDistance < range){
panTo(player, followSpeed);
} else {
panBehavior();
}
}

function panTo(target : Transform, speed : float){
var relativePos = target.position - transform.position;
var rotation = Quaternion.LookRotation(relativePos);

transform.rotation = Quaternion.Slerp(transform.rotation, rotation, Time.deltaTime * speed);
}

function panBehavior(){
var relativePos = currentPanTarget.position - transform.position;

var lookingAtLimit = false;
if(Vector3.Angle(relativePos, transform.forward) < 20){
lookingAtLimit = true;
}

// if needed swap direction
if(lookingAtLimit){
if(currentPanTarget == leftLimit){
currentPanTarget = rightLimit;
} else if(currentPanTarget == rightLimit) {
currentPanTarget = leftLimit;
}
}

panTo(currentPanTarget, panSpeed);
}

ey what you want is transform.eulerAngle.y (assuming your camera is rotating along the y axis). if you put that in a public variable, that will show what angle your camera is pointing (from 0 to 360 degrees). then use what eulerangles u want as the maximum range.