x


Rotate a Vector3 direction

How can i rotate a Vector3 direction 45 degrees along the y axis? So a Vector3(1,0,0) for example, would become Vector3(0.5,0,0.5).

And i cant use Transform.RotateAround, or any other Transform methos. As i am not applying this Vector3 directly to a object.

more ▼

asked Feb 08 '11 at 07:34 PM

Mattivc gravatar image

Mattivc
1.7k 55 60 67

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

2 answers: sort voted first

http://unity3d.com/support/documentation/ScriptReference/Quaternion.AngleAxis.html

vector = Quaternion.AngleAxis(-45, Vector3.up) * vector;

That's a general case. For rotation around a world axis, it's faster/easier:

vector = Quaternion.Euler(0, -45, 0) * vector;

For (1,0,0), this results in (sqrt(.5), 0, sqrt(.5)), not (.5,0,.5), by the way. The length of the rotated vector stays constant. To achieve a squared hypotenuse of 1, you add .5, 0, and .5, not .25, 0, and .25, which would shorten your vector.

more ▼

answered Feb 08 '11 at 08:04 PM

Jessy gravatar image

Jessy
15.6k 72 95 196

Thanks, this works great.

Feb 09 '11 at 08:04 PM Mattivc

Glad to hear it. Rotate those vectors like they were on the Witches' Wheel!

Feb 09 '11 at 09:47 PM Jessy
(comments are locked)
10|3000 characters needed characters left

That operation is dependent on the order. rotatedVector = Quaternion * vector OK rotatedVector = vector * Quaternion won´t works and will launch a compiler error

more ▼

answered Feb 10 at 03:00 PM

elhispano gravatar image

elhispano
1

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

x2165
x576
x211

asked: Feb 08 '11 at 07:34 PM

Seen: 10765 times

Last Updated: Feb 10 at 03:00 PM