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 /
avatar image
0
Question by Ay-mak · Feb 05, 2019 at 11:45 PM · velocityforward

Converting a Velocity

How can I convert a Velocity of a Forward of 0, 0, 1 to a new Velocity of another Forward? For exemple 0, 1, 0.

Basically, I'm used to get the velocity.x variable to know if the player is turning left or right. The problem is that some scenes have turns now, and the velocity.x variable is just not related anymore since it's respecting the world's forward wich is 0, 0, 1 I think.

When the player takes the map turn, the game changes it's Forward to fit to the new direction and I would like to create a new Velocity vector that would tell me the velocity based on the player's forward and not the world's forward.

I tried currentVelocity = currentForward * rb.velocity.magnitude; But that still gives me the world's velocity of the player

Comment
Add comment · Show 6
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 Ymrasu · Feb 06, 2019 at 12:27 AM 1
Share

You can rotate a velocity (Vector3) with a quaternion. Such as: rb.velocity = Quaternion.AngleAxis(30f, Vector3.up) * rb.velocity; , which rotates 30 degrees around the y axis (if y is up). If you are wanting to rotate the velocity when the map rotates, then apply that method with the same angle as the map. If this is not what you are talking about, please let us know.

avatar image Ay-mak Ymrasu · Feb 06, 2019 at 01:17 AM 0
Share

Thanks for your response. How can I get the rotation angle of Y from the forward ?

avatar image Ymrasu Ay-mak · Feb 06, 2019 at 01:28 AM 0
Share

Quaternion can get the rotation between two vectors (or in this case forwards) as well with: rb.velocity = Quaternion.FromToRotation(oldForward, newForward) * rb.velocity;

Show more comments

1 Reply

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

Answer by Bunny83 · Feb 06, 2019 at 02:29 AM

If you want to know the velocity in local space of your camera. Just use InverseTransformDirection of the transform of your camera with your worldspace velocity:

 public Transform camTransform;
 
 Vector3 localVelocity = camTransform.InverseTransformDirection(rb.velocity);

Now localVelocity.x is always "right" as seen from the camera.


If you just want to know if the velocity is pointing to the left or to the right of the camera, you could also use the vector dot product

 if ( Vector3.Dot(rb.velocity, camTransform.right) > 0 )
     Debug.Log("to the right");
 else
     Debug.Log("to the left");
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 Ay-mak · Feb 06, 2019 at 02:46 AM 0
Share

Thanks. Your code can actually be implemented directly to the rolling ball object with currentVelocity = transform.InverseTransformDirection(rb.velocity);

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

99 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

Related Questions

Change velocity direction 1 Answer

problem with dead space kinesis module system 1 Answer

Velocity powered rigidbody on a moving platform without parenting. 3 Answers

Rigidbody slowing right down when colliding only when I'm rotating the velocity to always be going forward 1 Answer

Calculating the difference between forward direction and velocity direction to set the body roll of my vehicle. 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