- Home /
How do I rotate on Z axis within a set of constraints?.
i'm trying to make something that can be rotated, but within a set of constraints, in my case, the Z axis.
however, any attempts made so far failed badly, here is my script, the context is that the W button is meant to tilt the platform upwards, but also only be able to go so far, the same is true for S, except it tilts backwards:
if (Input.GetKey (KeyCode.W) && platform.transform.rotation.eulerAngles.z <= 25f)
{
platform.Rotate(0f, 0f, 2f);
}
if (Input.GetKey (KeyCode.S) && platform.transform.rotation.eulerAngles.z >=330f)
{
platform.Rotate(0f, 0f, -2f);
}
what i want is for the rotation to go from 25 to 15 and then 5, then 0, then 359, then 350 and 330, that direction, and from 330 to 359 to 10 to 20 to 25 with the W button.
Answer by Ashutosh8126 · Feb 26, 2014 at 12:43 PM
After the two ifs you can use.. platform.eulerAngles = new Vector3(platform.eulerAngles.x,platform.eulerAngles.y,Mathf.Clamp(platform.eulerAngles.z,25,330));
hm, i've tried this now, it doesn't quite work, i mean, it does let me rotate and it does indeed clamp the platform, but the problem is that it does so in the wrong way.
see, this rotation is meant to be more like tilting back and forth, however, it requires me to rotate it all the way upside down and back to get from one freeze point to the other, which is very backwards and quite literally the opposite way it should work.
i'm looking now at how the rotation changes, and it seems that what i want is for the rotation to go from 25 to 15 and then 5, then 0, then 359, then 350 and 330, that direction, is that possible to do?.
Answer by Wolfram · Feb 26, 2014 at 04:35 PM
Your code for W should be fine, does it not work as expected?
The code for S also uses = here instead, as you don't want the angle to become smaller than 330. Querying something.eulerAngles will always return angles between 0 and 360.
Note also that transform.rotation.eulerAngles queries World coordinates, while the default settings for Rotate() modify local coordinates (see docs). You actually might want to use transform.localRotation.eulerAngles instead, or transform.localEulerAngles for short.
the W code seems to work now that i put that back in, but the S code doesn't, even with your suggested change, or the localEulerAngles.
looking at the way the rotation is working now, it seems that what i want is for the rotation to go from 25 to 15 and then 5, then 0, then 359, then 350 and 330, that direction, and from 330 to 359 to 10 to 20 to 25 with the W button.
Your answer
Follow this Question
Related Questions
Adding Rotation & Fire Rate to an Object Pool 0 Answers
Rotating, moving and keep on going with constant velocity in direction of click 1 Answer
how to move the camera using the mouse scroll in all direction? 0 Answers
Rotating a Gameobject In Three Axis Unity 3 Answers
Lock rotation of tank barrel 2 Answers