Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 Jun 22
sparklines
Close Help
  • 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
1
Question by PhoenixBlackReal · Dec 09, 2014 at 01:05 AM · vectorx-axis

Rotate an object so its x axis would align with a vector

It's 3 am, I'll try to be clear... transform.position is the vector start point, let's say A(x,y,z). B(a,b,c) is the end of the vector. Consider this vector as a line in space, direction is irrelevant. Now, how should I rotate the object I have so its x axis would align with the vector AB? I mean, through code, using c#. If I know A and B, how would I calculate the transform.rotate for the object? Thank you in advance.

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
9
Best Answer

Answer by robertbu · Dec 09, 2014 at 01:13 AM

 Vector3 v = B - A;
 Quaternion q = Quaternion.FromToRotation(transform.right, v);
 transform.rotation = q * transform.rotation;

Or simpler:

  Vector3 v = B - A;
  transform.rotation = Quaternion.FromToRotation(Vector3.right, v);

  


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 PhoenixBlackReal · Dec 09, 2014 at 01:21 AM 0
Share

You're awesome! Thank you, I'm going to crash in my bed now UwU But you, however, have an amazing day!

avatar image Fe0dor · Sep 08, 2020 at 09:14 PM 0
Share

6 years later and I too found myself in this situation... You saved my night

avatar image mscottveach · Sep 09, 2021 at 06:59 AM 0
Share

Exactly one year to the day later, I, too, find myself in the situation.

avatar image IsitaSpider · May 04 at 05:39 PM 0
Share

Still perfect in 2022, really helpful, thank you !

avatar image
2

Answer by David_Biggs · Dec 09, 2014 at 02:23 AM

transform.right = B-A;

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 ShawnFeatherly · Sep 21, 2017 at 11:56 PM 0
Share

Nice shortcut, and easier to understand than setting transform.rotation to some Quaternion.LookRotation() that would get the angle right.

avatar image
0

Answer by Vitaly-Abilevich · Sep 13, 2017 at 11:16 PM

Thanks, this works perfect, is there a way to add a Linear Interpolation?

 Vector3 v = go.transform.position - transform.position;
 Quaternion q = Quaternion.FromToRotation(-transform.up, v);
 // Changed next line for the last one
 //transform.rotation = q * transform.rotation;
 transform.rotation = Quaternion.Lerp(transform.rotation, q, Time.deltaTime * alignSpeed);

And with this change weird stuff happens

Comment
Add comment · 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

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

32 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 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

Calculating the Angle between two vectors 2 Answers

Rb2D.MovePosition not working - weird error... 1 Answer

"normalize" a vector but conserve the "- sign" 2 Answers

Is there a way to set a pallet for colours in a vector and change them at runtime for one gameobject? 0 Answers

Clamp Vector direction to one axis 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