Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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
0
Question by lukas_balaz · Aug 01, 2014 at 12:24 PM · vector3rotate

rotate vector3

I have Vector3 variable and I want to rotate it so that Y-axis will be pointing up. How to do it?

EDIT: sorry, Y-axis has to be 0 (so the resulting Vector is on XY plane)(but it has to have same magniude of course)

Comment
Add comment · Show 3
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 commodore · Aug 01, 2014 at 12:29 PM 0
Share

You can use this to point your Vector3 in a specific direction smoothly over time

http://docs.unity3d.com/ScriptReference/Vector3.SmoothDamp.html

avatar image lukas_balaz · Aug 01, 2014 at 01:25 PM 0
Share

I dont want to do it smoothly ... i want to calculate it

avatar image lukas_balaz · Aug 01, 2014 at 01:27 PM 0
Share

and what target do I should write as second parameter if I want to do it smoothly ??

2 Replies

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

Answer by lukas_balaz · Aug 01, 2014 at 02:10 PM

Found my answer:

 float mag = v.magnitude;
 v -= new Vector3 (0,v.y,0); // set y to 0
 v.Normalize();
 v *= mag //multiply by mag (so magnitude is same as before)
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
avatar image
0

Answer by robertbu · Aug 01, 2014 at 01:30 PM

Given a vector v at an arbitrary angle, youu can do:

  v = Quaternion.FromToRotation(v, Vector3.up) * 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 lukas_balaz · Aug 01, 2014 at 01:45 PM 0
Share

sorry, I meant that Y axis has to be 0 (so the resulting Vector is in XY plane), my mistake

avatar image robertbu · Aug 01, 2014 at 02:02 PM 0
Share

In that case, the easiest thing to do is to just set the 'y' value of the vector to 0.0. I don't know your application, but you might have to special case the situation when the vector is pointing straight up. Also depending on your application, you may want to normalize the result. Note for changing over time, you can use Vector3.Slerp(), Vector3.Lerp(), or Vector3.RotateTowards().

avatar image robertbu · Aug 01, 2014 at 02:04 PM 0
Share

Just saw your magnitude edit:

 var mag : float = v.magnitude;
 v.y = 0.0;
 v = v.normalized * mag;

For the special case of straight up, you can just create any vector on the xz plane with the right magnitude:

 var mag : float = v.magnitude;

 if (v == Vector3.up) {
     v = Vector3.right * mag;
 }
 else {
     v.y = 0.0;
     v = v.normalized * mag;
 }
avatar image lukas_balaz · Aug 01, 2014 at 02:11 PM 0
Share

sorry I read your comment 10 seconds after posting it as answer :)

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

23 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

Related Questions

How do I make gameObject.transform.rotation.z equal to a set float value? 2 Answers

Multiball Situation in Pong 0 Answers

Rotate Towards doesnt work as expected 2 Answers

Rotating objects around player using lerp 0 Answers

How to make object lean in the direction it's going 3 Answers


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