- Home /
Clamping Rotation not working
here is my script:
if(Input.GetMouseButton(1))
{
myTransform.Rotate(0, Input.GetAxis("Mouse X") * Time.deltaTime * 300f, 0);
myTransform.localRotation = Quaternion.Euler(0, Mathf.Clamp(myTransform.rotation.y, 20, 340), 0);
}
myTransform.rotation is a Quaternion...a non-intuitive 4D construct. You should not directly access the x,y,z,w components of a Quaternion unless you fully understand the math behind them. You can access myTransform.eulerAngles or myTransform.localEulerAngles, but depending on a specific representation of eulerAngles like you are doing here is risky and often fails.
Answer by supercouge · Jul 20, 2013 at 07:40 AM
if(Input.GetMouseButton(1))
{
myTransform.Rotate(0, Input.GetAxis("Mouse X") * Time.deltaTime * 300f, 0);
myTransform.localRotation = Quaternion.Euler(0, Mathf.Clamp(myTransform.rotation.eulerAngles.y, 20, 340), 0);
}
// Just like Robertu said, there is a huge difference between rotation.y and rotation.eulerAngles.y
Here you can found a good explanation of Quaternion and, in a more general matter, rotation representation in 3D computer graphic (p147).
Your answer
Follow this Question
Related Questions
Turrets acting oddly when clamped 1 Answer
Clamping RotateAround 1 Answer
Clamp rotation using Quaternion.Euler does not work 2 Answers
limit rotation using math.clamp 2 Answers