- Home /
[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);
}
Answer by Proportion · Oct 16, 2011 at 12:00 PM
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.
I tried implementing that in multiple ways and it does not seem to be working at all. Could you be a little more clear on how to implement it in the script? I am fairly new to euler angles and Quaternions. It might also help to know that the camera rotates in 3 dimensions. So it follows the player, left, right, and up and down. I have been banging my head off a wall trying to figure this out. It might be that I do not have enough experience with these math constructs to know what operations I actually need to detect if the player has gone outside the bounds.
Your answer
Follow this Question
Related Questions
Object Rotation doesnt work 0 Answers
Clamp Rotation Problem 1 Answer
Problem changing rotation of 3rd Person Controller 1 Answer