- Home /
Stop rotation of camera at certian degree
Ok so here is my third attempt but with no success. right now it just freezes the game after a few seconds and does not look any where just straight up. Is there something I missing with this? I am basing it off the mouse look at script in the standard assets. Here is what I have so far: Thanks for any help with this
var turnSpeed : float = 15f;
var minX :float = -90;
var maxX : float = 90;
var minY : float = -90;
var maxY :float = 90;
var rotationX : float = 0f;
var rotationY : float = 0f;
var originalRotation : Quaternion;
var xQuaternion : Quaternion;
var yQuaternion : Quaternion;
function Start () {
}
function Update () {
//rotate the comera
//this works
this.transform.RotateAroundLocal(Vector3.up, Input.GetAxisRaw("Vertical") * turnSpeed);
this.transform.RotateAroundLocal(Vector3.forward, Input.GetAxisRaw("Horizontal") * turnSpeed);
if(Input.GetAxis("Vertical"))
{
rotationX += Input.GetAxis("Vertical") * turnSpeed;
rotationX = ClampAngle(rotationX, minX, maxX);
}
if(Input.GetAxis("Horizontal"))
{
rotationY += Input.GetAxis("Horizontal") * turnSpeed;
rotationY = ClampAngle(rotationY, minY, maxY);
}
xQuaternion = Quaternion.AngleAxis (rotationX, Vector3.up);
yQuaternion = Quaternion.AngleAxis (rotationY, -Vector3.right);
transform.localRotation = originalRotation * xQuaternion * yQuaternion;
}
static function ClampAngle(angle :float, min : float , max :float )
{
if (angle < -360F)
angle += 360F;
if (angle > 360F)
angle -= 360F;
return Mathf.Clamp (angle, min, max);
}
Answer by konashadow · Dec 05, 2012 at 05:42 PM
You could try making a variable that keeps getting added or subtracted from depending on which way you turn, and only allow the object to keep rotating if the variable is in the allowed range.
how would I keep track of the current rotation value ?
At the end of the function, put (var) += 1. Start the camera at the center of the range you want it to turn, and make it rotate to the point you want it to be. Then check what the value of the variable is (make sure it is public). Then just put an if statement checking whether the variable is at the limit or not.
Your answer
Follow this Question
Related Questions
I was trying to have the camera track the mouse and this happened what is wrong??? 3 Answers
Character Mouse Look on Spine 2 Answers
Can't use camera.main [SOLVED] 1 Answer
Flip over an object (smooth transition) 3 Answers
How do I make it roll same speed, no matter what heights my orbit camera is looking at? 1 Answer