Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
  • Help Room /
avatar image
0
Question by xandermacleod · Oct 12, 2016 at 04:32 PM · anglevector2anglessinecosine

Convert a 360 bearing into a Vector 2

Hi there, I'm wondering how I can convert a bearing into a Vector2. I assume it should be fairly straight forward using Sin and Cos, but my maths isn't very good. The angle I would be passing into the functions works in the usual way a bearing works (i.e. clockwise starting from a 0 point at Vector2.up), and I need to get a Vector2 out with a magnitude of 1.

Any ideas?

Thanks

P.S. here's the code I have so far for it.

     Vector2 GetCameraNorth ()
     {
         //Find the forward facing vector of the camera (relative to the camera position) if it were on a 2d plane
         Vector2 CameraNorth = new Vector2 (Camera.main.transform.forward.x, Camera.main.transform.forward.z);
         Vector2 Offset = CameraNorth - new Vector2(Camera.main.transform.position.x, Camera.main.transform.position.z);
         return Offset.normalized;
     }
 
     Vector3 GetMoveDirection ()
     {
         //Get the player input as a 2d vector
         Vector2 MovementInput = new Vector2 (Input.GetAxis ("Horizontal"), Input.GetAxis ("Vertical"));
         Vector2 NormalizedMovementInput = MovementInput.normalized;
 
         //With both the Camera vector and the Input movement vector, we can calculate our desired direction by adding together the angles of both, and making a new vector using those angles.
         //To deal with the issue of angles in unity only being calculable between 0-180 (rather than 360) we have to do a little extra work to turn the angles into 360 degree bearings.
 
         float camBearing;
         float inputBearing;
         float desiredDirectionBearing;
 
         if (GetCameraNorth ().x < 0) {
             camBearing = 360 - Vector2.Angle (Vector2.up, GetCameraNorth ());
         } else {
             camBearing = Vector2.Angle (Vector2.up, GetCameraNorth ());
         }
         if (NormalizedMovementInput.x < 0) {
             inputBearing = 360 - Vector2.Angle (Vector2.up, NormalizedMovementInput);
         } else {
             inputBearing = Vector2.Angle (Vector2.up, NormalizedMovementInput);
         }
 
         desiredDirectionBearing = camBearing + inputBearing;
 
     }

In the above code, I would then be plugging in the float variable 'desiredDirectionBearing' to generate a Vector2 using it.

The basic idea being:

  • Turn Vector 'Av' into Float Angle 'Af'

  • Turn Vector 'Bv' into Float Angle 'Bf'

  • Make sure that both 'Af' and 'Bf' are 0-360 bearings rather than a 0-180 angles.

  • 'Af' + 'Bf' = 'Cf'

  • Turn Float Angle 'Cf' into Vector 'Cv' (this here being the step I dont know how to do).

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

1 Reply

· Add your reply
  • Sort: 
avatar image
1

Answer by elenzil · Oct 12, 2016 at 04:49 PM

 Vector2 unitLengthVec = new Vector2(cos(angle), sin(angle) * vectorLength;
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 Owen-Reynolds · Oct 13, 2016 at 12:54 AM 0
Share

That assumes a real angle: radians and 0=east, CCW. Otherwise I think convert to radians and swap sin/cos to get a game angle. Also sin/cos need $$anonymous$$athf-dot.

I feel like a more Unity way of doing this might be Quaternion.Euler(0,angle,0)*Vector3.forward*len;

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

59 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

Related Questions

Having trouble using Vector2.SignedAngle 2 Answers

Calculating the Angle of a Vector2 from zero 2 Answers

Instantiating Projectiles not working at certain directions?? 2 Answers

Problem with Mathf.Sin 0 Answers

Trying to clamp a second position to 45 degrees in 2d space 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