x


How to Set a Single Axis Rotation of a GameObject?

What is the most efficient way of setting a rotation of a single axis of a GameObject without changing the other values?

more ▼

asked Dec 14 '09 at 05:08 AM

Devin Reimer gravatar image

Devin Reimer
58 3 3 6

(comments are locked)
10|3000 characters needed characters left

4 answers: sort voted first

I think this is what you're looking for :

// Sets the transforms rotation to rotate 30 degrees around the y-axis
transform.rotation = Quaternion.AngleAxis(30, Vector3.up);
more ▼

answered Dec 14 '09 at 03:49 PM

PatHightree gravatar image

PatHightree
542 8 10 23

(comments are locked)
10|3000 characters needed characters left

Don't know what you exactly want but maybe this is it:

var rot = transform.rotation;
transform.rotation = rot * Quaternion.Euler(0, 90, 0); // this is 90 degrees around y axis
more ▼

answered Dec 14 '09 at 06:09 AM

Jaap Kreijkamp gravatar image

Jaap Kreijkamp
6.9k 21 30 74

this can be done easily by transform.rotate

Dec 28 '09 at 06:00 PM Ashkan_gc
(comments are locked)
10|3000 characters needed characters left

what jaap did can be done by transform.rotate and i think it's not what you want and you want to set the value directly. you can use his code but remove the "rot*" and then the code will be

transform.rotation = Quaternion.Euler(0, 90, 0);

if you want to add some value to any of the x,y,z values simply use

transform.Rotate (0,0,10);

this will add 10 to Z value

more ▼

answered Dec 28 '09 at 06:08 PM

Ashkan_gc gravatar image

Ashkan_gc
9.9k 36 59 123

(comments are locked)
10|3000 characters needed characters left

The rotations are stored as quaternions, so you can store the appropriate quaternion and set transform.rotation to that.

Perhaps an explanation of what you are trying to accomplish would help. If you go into "Debug mode" and watch the rotation values change, you can see how just changing one axis's value isn't really more efficient. It only looks that way in "Normal mode".

more ▼

answered Dec 14 '09 at 05:45 AM

Jessy gravatar image

Jessy
17.8k 78 101 205

(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

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

By RSS:

Answers

Answers and Comments

Topics:

x3220

asked: Dec 14 '09 at 05:08 AM

Seen: 35352 times

Last Updated: Jun 04 '12 at 08:25 PM