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 Marscaleb · Sep 08, 2020 at 01:36 AM · vector3angles

Convert an angle to a vector?

I've seen a number of people post this question, but every result is a different application, and I can't properly adapt their answers to my situation.

I have an angle (in degrees.) I want to apply it as a velocity to a rigidbody. Velocity values are stored as Vector3 values, so I need to convert this to a vector3. Of course, I also need to apply a magnitude or speed, and since we are working in 3D space there is technically a need for two angles, but for right now I'm just applying the velocity on the XZ plane. (Maybe in the future I will want to modify this to include a second angle so it can aim up and down?)

Looking at other answers, they use a quaternion with the method AngleAxis, but I can't apply a quaternion, I need a vector. I tried multiplying it by a vector3 to change it to a vector3, but that just changed it to the same Vector3 I multiplied it by for some reason.

Comment
Add comment · Show 1
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 Marscaleb · Sep 08, 2020 at 02:40 AM 0
Share

This is the code I used:

         thisShot.GetComponent<Rigidbody>().velocity = Quaternion.AngleAxis(facingAngle, Vector3.forward) * (Vector3.forward * 10f);

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Eno-Khaon · Sep 08, 2020 at 02:19 AM

I don't really see how Quaternion*Vector multiplication wouldn't give you the result you're looking for here:

 Vector3 straight = Vector3.forward;
 // Vector3.up for clockwise-based rotation
 Quaternion r90 = Quaternion.AngleAxis(-90f, Vector3.down);
 Debug.Log(straight + " -> " + (r90 * straight));
 // Output: (0, 0, 1) -> (1, 0, 0)


Take the result and scale it as needed and you'll have your force to apply:

 Quaternion angleRotation = Quaternion.AngleAxis(desiredRotation, Vector3.down);
 // Using a non-fixed baseline as an example (transform.forward)
 Vector3 forceDirection = angleRotation * transform.forward;
 // Example applying an immediate force, unaffected by mass
 rb.AddForce(forceDirection * speed, ForceMode.VelocityChange);
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

170 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 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 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 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 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

How to project an angle from a vector 3 Answers

Vector3.SignedAngle changes sign for no reason 1 Answer

How to calculate the rotation of a hexagon based on its six vertices position 2 Answers

Change a property based on angle? 1 Answer

Vector3.SignedAngle seemingly avoids values close to 0 and +-180 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