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, 2009 at 05:08 AM

Devin Reimer gravatar image

Devin Reimer
73 7 6 9

(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, 2009 at 03:49 PM

PatHightree gravatar image

PatHightree
572 22 19 36

(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, 2009 at 06:09 AM

Jaap Kreijkamp gravatar image

Jaap Kreijkamp
7k 43 45 93

this can be done easily by transform.rotate

Dec 28, 2009 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, 2009 at 06:08 PM

Ashkan_gc gravatar image

Ashkan_gc
10k 76 91 160

(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, 2009 at 05:45 AM

Jessy gravatar image

Jessy
18.1k 207 173 283

(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:

x3495

asked: Dec 14, 2009 at 05:08 AM

Seen: 40834 times

Last Updated: Jun 04, 2012 at 08:25 PM