# Rotation using Quaternions

 0 I have a slider that I use to choose how much I want the object rotated between -180 to 180. Using this code below makes rotation on the X axis possible: ``````Quaternion rot = Quaternion.AngleAxis(rotation, new Vector3(1,0,0)); transform.localRotation = rot; ``````HOWEVER, I want the X rotation on the localRotation to be the only one that changes. Y and Z rotations should be completely unaffected. On top of that, X should reset the X rotation before setting the new X rotation, or else the slider would add the value as you slide it, instead of indicating the value. Using Eulers does not work! Anything not between -90 to 90 gives me unpredictable results, changing the Y and Z rotations to 180, and doing other weird stuff I don't fully understand. How do I reset ONLY the x rotation, then set ONLY the x rotation with the newly calculated x rotation??? more ▼ asked Jul 17 '12 at 05:44 PM DoctorWhy 211 ● 5 ● 16 ● 30 add new comment (comments are locked) 10|3000 characters needed characters left ▼ Viewable by all users

3 answers: sort voted first
 0 The Y and Z changing is not really something you can prevent, if you're talking about the euler angle values. Rotations are stored as quaternions, and there's more than one valid way to represent a rotation as a euler angle (for example, 0, 0, 0 is the same as 180, 180, 180). However, you can simply keep track of it yourself...assuming you have a float called xRotation, use this in OnGUI: ``````xRotation = GUI.HorizontalSlider (new Rect(10, 10, 200, 20), xRotation, -180, 180); transform.localEulerAngles = new Vector3(xRotation, 0, 0); `````` more ▼ answered Jul 17 '12 at 08:37 PM Eric5h5 80.3k ● 42 ● 132 ● 521 Please read my question before answering. This RESETS Y and Z rotations to 0 (or switches them to 180). I want the Y and Z rotations to be left untouched. I know messing with the Eulers generally provides these types of results, which is why I want to use Quaternions. Jul 17 '12 at 09:19 PM DoctorWhy I did read your question. Just add in the Y and Z values (which should also be your own variables, since you can't read from euler angles reliably) rather than using 0, 0, which was just an example in order to focus on how to set the X rotation. Jul 17 '12 at 10:32 PM Eric5h5 But if the Y and Z are 0 (I haven't tested any other values yet), they will get changed to 180 if you go out of the -90 to 90 bounds. I want them to STAY 0. Could I reliably do that using transform.localEulerAngles? Jul 17 '12 at 10:46 PM DoctorWhy No, if you assign 0 to your Y and Z variables, they won't somehow change. The Y and Z values in the inspector probably will, but that won't be relevant, since you wouldn't read those values anyway. Your "internal" rotation values will be stored by you in your own variables, so will always be consistent. Jul 17 '12 at 10:56 PM Eric5h5 Ah, ok... It worked. See... I thought I tried this, but I guess I only tried just the X and noticed the y and z changing and thought it wouldn't work. Sorry if I seemed a bit short tempered... rotation was harder in Unity than any other libraries or game engines I have ever used. Thanks for the help! Jul 18 '12 at 03:07 AM DoctorWhy add new comment (comments are locked) 10|3000 characters needed characters left ▼ Viewable by all users
 0 Save your original facing: `Quaternion F = transform.rotation;`. Do that whenever you go into "rotate on X mode."You already have the desired x rotation set up, in `rotation` -- a float which you somehow move between -180 to 180.Last step is to say your new rotation is your starting, with the additional X-rotation added on: `transform.rotation = F * Quaternion.Euler(rotation, 0, 0);`NOTES:o That says to start at your original facing F and "apply" the local x-tilt. Star is redefined for quaternions to mean "rotate like this, then from there like this."o `Quat.AngleAxis` is general purpose, if you want to rotate around a non-axis. `Quat.Euler` is the short version, for axis-rotations.o If you flip the order: `transform.rotation = Quaternion.Euler(rotation,0,0,) * ;`, then you'll perform a world rotation on x, instead of local. more ▼ answered Jul 17 '12 at 08:53 PM Owen Reynolds 11.5k ● 1 ● 7 ● 45 Doesn't this rotate from the old x rotation??? I don't want to ADD to the old X rotation, I want the new X rotation EXACTLY equal to the new X rotation without touching the old Y and Z rotation. Jul 17 '12 at 09:15 PM DoctorWhy add new comment (comments are locked) 10|3000 characters needed characters left ▼ Viewable by all users
 0 Sounds like a job for Slerp(). Given two rotations and a float between 0 and 1, slerp will produce a new rotation X% between the other two. It's the same principle as lerping (linear interpolation), which will probably be an easier search term if this is a new topic for you. more ▼ answered Jul 17 '12 at 07:13 PM rutter 5.2k ● 2 ● 11 This is still going to Slerp to the X rotation being correct but the Y and Z rotation resetting, is it not?????? I need JUST X to change, not Y or Z Jul 17 '12 at 07:40 PM DoctorWhy add new comment (comments are locked) 10|3000 characters needed characters left ▼ Viewable by all users

### Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here