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
3
Question by Alkan · Nov 02, 2010 at 01:19 PM · rigidbodyvelocityaxislocal

How do I obtain the local velocity of a Rigid body on one axis?

I'm creating a simple driving game were the player controlled vehicle is a single box-like rigid body.

I am accelerating the vehicle with rigid.body.AddRelativeForce and I'm steering the object with rigid.body.MoveRotation.

I really need to be able to obtain the relative velocity along the vehicles local X axis (forwards and backwards) to be able to script rules and logic from, and right now all I seem able to do is get either the world velocity (rigidbody.velocity) or all-axis-combined velocity (rigidbody.velocity.magnitude) but neither of these are accurate enough for what I want to do.

Can anyone offer any hints here?

Thanks.

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

Answer by Jesse Anders · Nov 02, 2010 at 03:59 PM

Try (untested):

float xVel = transform.InverseTransformDirection(rigidbody.velocity).x;
Comment
Add comment · Show 12 · 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 VS48 · Nov 02, 2010 at 04:08 PM 0
Share

Just tested it, it works -- I've been doing this manually all this time. Thanks!

avatar image Atnas1010 · Nov 02, 2010 at 04:10 PM 0
Share

That does exactly the same as the code I posted... (which works btw, just tested)

avatar image VS48 · Nov 02, 2010 at 04:30 PM 1
Share

I don't seem to be getting the same result

avatar image Atnas1010 · Nov 02, 2010 at 04:38 PM 0
Share

Hmm, I'm looking into that right now

avatar image VS48 · Nov 02, 2010 at 04:50 PM 0
Share

Hehehe this should work: Quaternion.FromToRotation(transform.forward, Vector3.forward) * rigidbody.velocity); It was correct, just I think one of the axes was inverted

Show more comments
avatar image
4

Answer by Atnas1010 · Nov 02, 2010 at 02:19 PM

Is this what you are looking for?

(transform.rotation * rigidbody.velocity).x

Comment
Add comment · Show 2 · 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 Mr-Mechanical · Jun 19, 2015 at 08:04 PM 0
Share

This works, but for folks looking back to see how to achieve this in unity 5 use:

 private Rigidbody rb;
 //in the start function
 rb = GetComponent<Rigidbody>();
 //in the update loop 
 rotation = (transform.rotation * rb.velocity).x;

P.S. I am a newbie to C# so please correct me if I have made a mistake

avatar image FlightOfOne · May 21, 2019 at 03:43 AM 0
Share

I would like clarification on this. I believe this answer is more 'performant' than
float xVel = transform.InverseTransformDirection(rigidbody.velocity).x;? is that correct? for example if called in every frame.

avatar image
-1

Answer by e-bonneville · Nov 02, 2010 at 01:40 PM

Use rigidbody.velocity.x. Is that what you're looking for?

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 Alkan · Nov 02, 2010 at 02:06 PM 0
Share

No, rigidbody.velocity.x gives me the velocity on the world x axis, not the local axis - this means it can report anything from high to low values depending on the direction I'm driving the vehicle in.

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

Velocity relative to Local Axis 1 Answer

rigidbody velocity on x axis and addforce on y axis ?? 1 Answer

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

Movement behaves differently on X axis 0 Answers

Get axis of bullet for rigidbody.velocity 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