• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
50
Question by Mattivc · Feb 08, 2011 at 07:34 PM · rotationvector3math

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.

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

3 Replies

· Add your reply
  • Sort: 
avatar image
143
Best Answer

Answer by Jessy · Feb 08, 2011 at 08:04 PM

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.

Comment
Add comment · Show 16 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Mattivc · Feb 09, 2011 at 08:04 PM 0
Share

Thanks, this works great.

avatar image Jessy · Feb 09, 2011 at 09:47 PM 2
Share

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

avatar image beneon · Sep 14, 2014 at 02:34 PM 0
Share

really thanks a lot

avatar image Thaoren · Nov 02, 2018 at 01:47 AM 3
Share

Thanks! Worked great.

One thing to note is that you cannot do:

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

and must do

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

as there are some reasons internal to Unity.

avatar image Bonfire-Boy Thaoren · Jul 01, 2019 at 08:55 AM 3
Share

as there are some reasons internal to Unity.

Nope, reasons internal to mathematics. You wouldn't say that 2/3 and 3/2 are different "because Unity", would you?

avatar image ratneshpatel Thaoren · Sep 21, 2019 at 12:40 PM 0
Share

That is because Quaternion $$anonymous$$ultiplication, A x B is not B x A. This is just the tip of the iceberg when you talk about complexity and beauty of quaternions.

avatar image Bunny83 ratneshpatel · Sep 21, 2019 at 02:44 PM 3
Share

Right, though Unity just decides to only implement Quaternion * Vector multiplication. Mathematically in order to rotate a vector you have to multiply the quaternion on the left side of the vector and the complex conjugate on the right side which is all implicit in that one multiplication operator in Unity. To see an actual run-down I always recommend this Numberphile video. It does simplify the concept a lot, but you get a clue what's actually going on. I once manually derived the quaternion - vector multiplication myself by literally expanding and then simplify the following

 (q.w + q.x*i + q.y*j + q.z*k) * (v.x*i + v.y*j + v.z*k) * (q.w - q.x*i - q.y*j - q.z*k)

though always remember the rules i² = j² = k³ = ijk = -1 and the order. So i*j == k but j*i == -k. i,j and k form a "ring". Writing them in a circle clockwise and you can derive the sign easily.

 // clockwise
 i*j = k
 j*k = i
 k*i = j
 // counter clockwise
 k*j = -i
 j*i = -k
 i*k = -j

in the end the real parts should cancel and you're left with just i,j and k which is your rotated vector.


Here's my run-down. The resulting equations essentially form a 3x3 matrix.

Show more comments
avatar image tormentoarmagedoom · Jan 29, 2019 at 09:35 PM 3
Share

Good question, good answer

Show more comments
avatar image
22

Answer by elhispano · Feb 10, 2013 at 03:00 PM

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

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image beneon · Sep 14, 2014 at 02:35 PM 0
Share

ok, Got it.

avatar image
2

Answer by EliasMiettinen · Apr 08, 2021 at 04:16 AM

You can set the your object rotation as Vector with euler like this:

 Vector3 newRot = new Vector(0f, 90f, 0f);
 transform.rotation = Qauternion.Euler(newRot);
Comment
Add comment · Show 4 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Riiich · Jan 05 at 10:48 AM 0
Share

This is what I was looking for! Works when using the inspectors "Copy Rotation" when right clicking on the Transform and needing to use the rotation from there.

avatar image Riiich · Jan 05 at 10:50 AM 0
Share

Though I just saw it was misspelled, this is the correct spelling:

 transform.rotation = Quaternion.Euler(new Vector(0f, 90f, 0f));
avatar image nateK06 Riiich · Mar 24 at 05:32 PM 0
Share

I'm sort of new to unity, but when I put new Vector(0f, 90f, 0f) I get an error saying "Vector" namespace couldn't be found. What am I doing wrong?

avatar image Bonfire-Boy nateK06 · Mar 24 at 05:41 PM 0
Share

There's a(nother) typo. It's Vector3 not Vector



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

Your answer

Hint: You can notify a user about this post by typing @username

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

Answers Answers and Comments

18 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Rotate object based on hit normal + matching camera direction 0 Answers

Rotate an object so its up vector matches a sphere 1 Answer

Unable to fully rotate GameObject on tap 2 Answers

VR: Controller rotation delta along other axis 2 Answers

How to rotate an object to align with the scenery 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges