- Home /
Solved it.
Rotation doesn't stop after hitting bounds
I have a simple problem I just don't understand. I want to rotate my camera up and down (around x-axis) with a 0° and 90° as bounds. Here is the part of the script:
if (Input.GetKey(KeyCode.W) && this.transform.localRotation.x > 0f)
{
this.transform.Rotate(Vector3.left * 0.5f * rotateSpeed * Time.deltaTime);
}
if (Input.GetKey(KeyCode.S) && this.transform.rotation.x < 90f)
{
this.transform.Rotate(Vector3.right * 0.5f * rotateSpeed * Time.deltaTime);
}
When I rotate it with "W" it stops when rotation hits 0. When I rotate it with "S" it just keeps rotating untill I release "S". Any suggestions?
in the first if you are using localRotation.x in the second one you are using rotation.x Is this intended?
tried both does not matter
you would get the same result UNLESS this object was the child of another rotated object.
Answer by Glurth · Feb 18, 2015 at 03:45 PM
Don't think you want to check the rotation.x ... This is a Quaternion (x,y,z,w) value, NOT a rotation around an axis. To check rotation around an axis, you want to look at the EulerValues of the transform.rotation. http://docs.unity3d.com/ScriptReference/Transform-localEulerAngles.html
e.g (this.transform.localEulerAngles.x < 90f)
or (this.transfor.rotation.eulerAngles.x)
//the Quaternion class version
thanks but now it works for s but not for w. When I mix it it gets stuck when it stops at 0
I think see what you mean, but I'm not really clear. What are you still using in your if statements now?
Answer by HexadimensionalerAlp · Feb 18, 2015 at 08:22 PM
Here is my final code:
if (Input.GetKey(KeyCode.W) && (cam.transform.localRotation.x > 0f && cam.transform.localRotation.x < 91f))
{
cam.transform.Rotate(Vector3.left * 0.5f * rotateSpeed * Time.deltaTime);
}
if (Input.GetKey(KeyCode.S) && (cam.transform.localEulerAngles.x < 90f || cam.transform.localEulerAngles.x > 350f))
{
cam.transform.Rotate(Vector3.right * 0.5f * rotateSpeed * Time.deltaTime);
}
Works this way, the new bounds are because it doesn't stop at 0° but around 359° so I had to change that. But I don't know why it only works with mixing localEulerAngles and Rotation.
well you are using X for both keys. I would suspect you'd want one of them to be another axis. see my edited answer above. Edit: oh, I see now.. the two keys are for back and forth on the same axis. ignore my comment & edit.
Follow this Question
Related Questions
Flip over an object (smooth transition) 3 Answers
Roll a ball camera problem? 1 Answer
Multiple Cars not working 1 Answer
How to Change rotation while preserving local horizontal rotation 1 Answer