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
1
Question by Sendatsu_Yoshimitsu · Oct 23, 2016 at 12:00 PM · vector3lerpmathfloat

How do I convert speed and heading to a vector?

I'm afraid this might be more rudimentary arithmetic than programming, but vector math has me really confused. Currently I have the following:

 Vector3 currentPosition;
 float currentHeading; //value clamped between 0-360 
 float currentSpeed; //measured in Unity Units per second

Every update, I need to move currentPosition in the direction of currentHeading at a rate of currentSpeed. I know the movement itself can be achieved by converting heading and speed into some vector newPosition, and multiplying (currentPosition*newPosition*Time.deltaTime), but how do I actually convert heading and speed into a single vector?

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

Answer by Downstream · Oct 23, 2016 at 12:26 PM

@Sendatsu_Yoshimitsu It's trigonometry. Assuming your heading is from up 0, right 90 degrees. You need the sin and cos functions

x = Mathf.Sin(angle * Mathf.Deg2Rad),

y = Mathf.Cos(angle * Mathf.Deg2Rad),

Vector2 movement = new Vector2(x,y) * speed * Time.deltaTime

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 Sendatsu_Yoshimitsu · Oct 23, 2016 at 12:58 PM 0
Share

Ooh, thank you very much! To confirm, once I have the movement vector, I would apply it with Vector3.Scale(), right? Like the following?

Vector3 vesselPosition = Vector3.Scale(vesselPosition, movement);

That's what I'm doing in update right now, what ends up happening is the X and Y values quickly lerp back to 0 regardless of heading or speed.

avatar image
0

Answer by Owen-Reynolds · Oct 23, 2016 at 04:03 PM

A more Unity-style way is to use the built-ins. The first line takes an arrow pointing north (forward) and spins it around y by your angle (it handles the sin/cos math for you):

 // length 1 in proper direction:
 Vector3 v1 = Quaternion.Euler(0,currentHeading,0)*Vector3.forward;
 // correct speed (same as last line in Downstream's example):
 v1=v1*currentSpeed;

For fun, since you claim to be confused about vector math in general, I put something (almost a book, but small chapters) at taxesforcatses.com/vectorW/TOC.html

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 paul-masri-stone · Feb 16, 2021 at 10:26 PM

Given the OP has currentHeading as a float, I'm guessing this is 2D. Here's my solution:

 // Calculate the currentHeading as a rotation about z axis
 Quaternion headingRotation = Quaternion.AngleAxis(currentHeading, Vector3.forward);
 
 // Apply that rotation to a vector scaled to the currentSpeed
 Vector2 velocity = headingRotation * Vector2.up * currentSpeed;
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

67 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

Related Questions

Non physics determinism of floating point math across platforms 1 Answer

how to calculate the angle between two vectors? 6 Answers

How to divide big float 2 Answers

Convert 2D direction vectors to 3D vectors around sphere 2 Answers

Clamp01 does not work? 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