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
3
Question by Johan 4 · Mar 26, 2011 at 12:42 PM · rotationvector3convert

How do I convert angle to vector3?

For example I have an object that I want to move at 45 degrees upwards. How do I get the vector coords that I need to push the object at? Thanks

- I want to MOVE the object. Not rotate, MOVE IT. So it moves diagonally 45 degrees. 45 degrees is just an example, I need to find how to convert from angle to vector. I do not want to rotate it, just move.

Comment
Add comment · Show 2
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 AngryOldMan · Mar 26, 2011 at 12:48 PM 0
Share

I think what your after is quaternions but I don't fully understand what you mean by you want to move an object at 45 degrees upwards??? Do you mean you want to rotate an object so it faces up 45 degress than it was before? Do you mean you want it to move in a diagonal direction that is 45 degrees between the x axis and the y axis? What do you mean by you want to push the object? Do you want to push the object in a direction with the player or move the object using keyboard buttons? Please update your question to be more specfic.

avatar image Johan 4 · Mar 26, 2011 at 12:54 PM 0
Share

Ok done, hope that helps

1 Reply

· Add your reply
  • Sort: 
avatar image
17

Answer by Bunny83 · Mar 26, 2011 at 02:24 PM

Bobadebob is right. Everything that involves rotations/angles you need a Quaternion. Just create a quaternion that rotates 45 upwards and multiply your vector2 with it to rotate the direction.

Use Quaternion.AngleAxis to create your quaternion.

var rot = Quaternion.AngleAxis(45,Vector3.right); // that's a local direction vector that points in forward direction but also 45 upwards. var lDirection = rot * Vector3.forward;

// If you need the direction in world space you need to transform it. var wDirection = transform.TransformDirection(lDirection);

You can of course set up the vector the old, traditional way :D. Just some trigonometry (that's the day your math teacher was talking about)

var angle = 45.0;
var lDirection = Vector3(0, Mathf.sin(Mathf.Deg2Rad * angle), Mathf.cos(Mathf.Deg2Rad * angle));
Comment
Add comment · Show 7 · 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 Mike 3 · Mar 26, 2011 at 02:25 PM 0
Share

small note - rot * vec, not the other way around

avatar image Bunny83 · Mar 26, 2011 at 02:46 PM 0
Share

You're right :D there's only an operator for Quaternion * Vector3. I'll fix that

avatar image behdadsoft · Aug 31, 2018 at 04:02 PM 0
Share

Hi @Bunny83

I need know one thing in your code, i seen a tutorial that has a code like your code. This is code:

     public Vector3 DirFromAngle(float angleInDegree, bool angleIsGlobal)
     {
         if (!angleIsGlobal)
         {
             angleInDegree += transform.eulerAngles.y;
         }
 
         return new Vector3($$anonymous$$athf.Sin(angleInDegree * $$anonymous$$athf.Deg2Rad), 0, $$anonymous$$athf.Cos(angleInDegree * $$anonymous$$athf.Deg2Rad));
     }

I don't understand why used Sin and Cos. and I need know why used Sin for X axis and used Cos for Y axis? Could you please explain me?

Tutorial link: link text

avatar image TimBur behdadsoft · Aug 31, 2018 at 04:25 PM 0
Share

This is a math question more than a program$$anonymous$$g question. The sin and cos functions are part of a branch of math called trigonometry. If you're really interested in gamemaking, it would be worth taking the time to learn trig. $$anonymous$$han Academy has a useful set of lessons here.

avatar image behdadsoft TimBur · Aug 31, 2018 at 08:00 PM 0
Share

Thanks for link. I made some games, but i accept my mathematical is weak. i now Y axis is sine and X axis is Cosine in Circle Trigonometry. i know some formula for change Degree to radians and vice versa. I need know Small things because i'm confused, if possible please answer my some little question. because i'm sure your answers help me to better understand.

1- I think this code want calculate angle in radians. right?

2- I know in Vector3, (x,y,z) must be float numbers so sine and cosine return a float number.Sine and Cosine Used for this reason?

3- why sine used for X axis and cosine for Z axis?

Thanks.

Show more comments

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

combine / convert rotations (Quaternion and Vector3) 1 Answer

how to convert the angle to roatation? 1 Answer

Transform a Vector by a Quaternion 1 Answer

Rotate One Object Around Another 1 Answer

slowly rotate a object *need quick fix* 0 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