- Home /
Limit rotation on x & y axis
I'm working on a project where a set of eyes rotate towards the cameras current position. However, I would like to limit the movement of the eyes on the x & y axis as I am trying to make the eye movement look as natural as possible. Thank you for your time.
Comment
"I would like to limit the movement of the eyes on the x & y axis"
Sorry if I didn't make myself clear Graham.
Best Answer
Answer by aldonaletto · Jun 20, 2013 at 11:22 PM
If you want to limit rotation inside a cone (same angle from the center in any direction), use Vector3.Angle to know the angle relative to the eye forward direction - for instance:
var maxAngle: float = 15;
private var fwdDir: Vector3;
function Start(){
fwdDir = transform.forward; // save initial forward direction
}
function Update(){
// find direction to camera
var dir: Vector3 = Camera.main.transform.position - transform.position;
// if camera inside viewing angle, follow it:
if (Vector3.Angle(dir, fwdDir) < maxAngle){
transform.forward = dir;
}
}