- Home /
Lock rotation on Z Axis
I have an object which follows the mouse position influencing his own rotation.
Like so:
void MouseRotator () {
if(transform.localEulerAngles.z <20.0f && transform.localEulerAngles.z >340.0f ){
var mouse = Input.mousePosition;
var screenPoint = Camera.main.WorldToScreenPoint(transform.localPosition);
var offset = new Vector2(mouse.x - screenPoint.x, mouse.y - screenPoint.y);
var angle = Mathf.Atan2(offset.y, offset.x) * Mathf.Rad2Deg;
transform.rotation = Quaternion.Euler(0, 0, angle);
}
}
And for some reason I'm not being able to lock rotation on >20 and <340.
What is the best way to do it? I'm not very familiar with EulerAngles.
Answer by Wolfram · Jan 29, 2015 at 05:15 PM
Your "if" demands z to be both < 20 and > 340 at the same time. Use an "or" instead:
if(transform.localEulerAngles.z <20.0f || transform.localEulerAngles.z >340.0f ){
...
I'm wondering how I haven't seen it... thanks sir for pointing that out.
Now i can proceed in locking the rotation, since the way it is, is just blocking rotation after passing those values.
Appreciated
Your answer
![](https://koobas.hobune.stream/wayback/20220613181902im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Rotating Sphere With Mouse 2 Answers
How to rotate an object's X, Y, Z based on mouse movement 4 Answers
Screen.lockCursor messes my rotation 1 Answer
Geometry Wars game experience in Unity 2 Answers
Camera Zoom from rotation. 0 Answers