- Home /
how do i Limit camera rotation on Y-Axis?
How can I limit the rotation on My y-axis for My camera? My movement code is:
var rotateSpeed : float = 3.0;
function Update () {
transform.localEulerAngles.x -= Input.GetAxis("Mouse Y") * rotateSpeed;
}
Thanks in advance!
Complete. Besides the update function it's within, This script is just for my camera to rotate vertically with $$anonymous$$ouse Y.
Answer by robertbu · Mar 12, 2013 at 06:45 PM
First, a quote from the reference on [localEulerAngles][1]:
Only use this variable to read and set the angles to absolute values. Don't increment them, as it will fail when the angle exceeds 360 degrees.
Typically the way I write code to deal with eulerAngles and localEulerAngles is to maintain my own Vector3 and assign but never read from eulerAngles. For most rotations, there are more than one euler angle representation, so you cannot depend on the values.
var v3Rotate = Vector3.zero;
var min = -50;
var max = 50;
var rotateSpeed : float = 45.0;
function Start() {
transform.localEulerAngles = v3Rotate;
}
function Update () {
v3Rotate.x += Input.GetAxis("Mouse Y") * rotateSpeed * Time.deltaTime;
v3Rotate.x = Mathf.Clamp(v3Rotate.x, min, max);
transform.localEulerAngles = v3Rotate;
}
Actually now that I reread your question, you want to limit the Y axis rotation, but your code only rotates around the X axis. Can you clarify your question?
Yes i know, I only put this script on my camera. I have the character movement script on my character. I want my character to turn horizontaly. And my camera moves vertically. I just copied the code off, of another answer page. But it seems to work fine. I will go ahead and try this code and tweak it to see if it works. Thanks!
Hey, I've got the same problem! Which code did you use? Could you share it?
Answer by pset · Jul 25, 2017 at 02:30 PM
You can use Quaternion.RotateTowards(Quaternion from, Quaternion to, float maxDegreesDelta) function to restrict rotation:
Quaternion ClampRotationYByLookDirection(Quaternion curentRotation)
{
Vector3 lookDirectionY = Vector3.ProjectOnPlane(lookDirection, Vector3.up).normalized;
Quaternion lookRotation = Quaternion.LookRotation(lookDirectionY);
Quaternion towardsRotation = Quaternion.RotateTowards(lookRotation, curentRotation, lookDeltaY);
return towardsRotation;
}
Your answer
Follow this Question
Related Questions
Rotation like in editor 2 Answers
Rotation in 8 way direction 2 Answers
Rotate and Move towards Mouse Point 0 Answers
Lock the Camera's Y Rotation Axis? 0 Answers
Need help in setting a game-object's rotation to euler-degrees. 0 Answers