Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
11 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
1
Question by Highwalker · Jul 20, 2015 at 08:38 AM · rigidbody2dvelocityphysics2dspeed

How to get the speed of an object?

So I know that you can get the velocity from the rigidBody. And I tried this. The problem I'm having, is that when I collide with an object, blocking the player, the velocity doesn't come back as 0, instead it is as if the player never stopped moving. Same thing happen's in reverse, when something collides with the player, causing it to move, the rigidBody.velocity still returns as greater than zero. Same thing happens in reverse. So when an object collides with the player while the player is standing still, causing it to move, the velocity returns as 0. I'm guessing it is because I change the velocity parameter of the player using the on screen controls. So how can I make it so that the player's speed comes back as 0 when he collides with something, and also make it not come back as zero when something else pushes the player?

I tried using:

 speed = (transform.position - lastPosition).magnitude / Time.deltaTime;
 lastPosition = transform.position;

But if I put it in the Update, it mostly comes back as 0 regardless if the player is moving or not. In LateUpdate, it will come back as zero every other Update. And in the FixedUpdate, it will work correctly for like 2 seconds, but then it starts throwing 2's for no reason.

Comment
Add comment · Show 3
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 Torigas · Jul 20, 2015 at 08:43 AM 1
Share

Try the physics materials? Something like sticky might do what you want.

You should not directly change the velocity either. Better use "AddForce" or something.

avatar image LeonardNS · Jul 20, 2015 at 11:01 AM 0
Share

What are you looking for? SpeedPerSec, distance moved that frame maybe?

avatar image meat5000 ♦ · Jul 20, 2015 at 11:57 AM 1
Share

Using your routine there gives me 2 to 4 times the speed value compared to velocity.magnitude, even when I use rigidbody.position.

But for all intents and purposes it works except for being inaccurate on curves. rigidbody.velocity is probably more accurate as it was the only thing in the debug which actually showed a 0 when reversing my direction.

I don't experience the FixedUpdate problem. There's something up there.

You must remember that velocity is directional but speed isnt. Thats why velocity is a vector and speed is a scalar.

Taking this in to account I think its pretty slim you'll actually ever get a perfect 0 for speed on a collision unless it is literally going EXACTLY back the way it came; and then the chances of getting your Debug to show that 0 are pretty slim as the time that object is at 0 for is too small. Given frame ti$$anonymous$$g the physics engine may have decided that your speed is already greater than 0 by the point your next debug comes about.

Imagine a powerful explosion. If the projected objects are held at 0 for a whole frame how is that going to look? (Hmm maybe actually quite good :/)

You may have better luck checking for 0 crossing points on each individual axis, in my opinion.

3 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by LeonardNS · Jul 20, 2015 at 11:10 AM

You could try

 speedPerSec = Vector3.Distance (oldPosition, transform.position) / Time.dealtTime;
 speed = Vector3.Distance (oldPosition, transform.position);
 oldPosition = transform.position;

but I don't see how it would really make a difference.

NOTE: I wrote both speed and speedPerSec, but you can just remove one of them and the other won't be affected.

Comment
Add comment · Show 3 · 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 Bradybeast128 · Jan 17, 2018 at 03:02 AM 0
Share

Can you tell me what the oldPosition is. Is it a vector3? please provide the code.

avatar image logicandchaos Bradybeast128 · May 10, 2021 at 12:09 PM 0
Share

it would have to be a vector3 or you could not assign one to it.

avatar image nohumanman · May 27 at 01:38 PM 0
Share

Time.deltaTime*

avatar image
0

Answer by Highwalker · Jul 21, 2015 at 08:10 AM

Thanks torigas! Using AddForce was almost the right thing, but does greatly improve the way my player moves overall. It was still a bit odd, and I had to achieve my desired effect in a completely different way than I had previously tried. But after long hours, I finally achieved what I was aiming for. Thank you everyone for your help :)

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 Jozi-Satler · Feb 06, 2021 at 06:24 PM

This should work:

float speed; Vector3 oldPosition;

 void FixedUpdate()
 {
     speed = Vector3.Distance(oldPosition, transform.position) * 100f;
     oldPosition = transform.position;

     Debug.Log("Speed: " + speed.ToString("F2"));
 }
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 yarielma272010 · May 10, 2021 at 09:21 AM 0
Share

How can you use "speed.ToSring("F2")" to set to a variable for a speedometer?

avatar image McPeppergames · May 27, 2021 at 01:50 PM 0
Share

I get different results with the Debug.Log output when viewing in full screen game mode or just a small window. How can I make this exact same value with all screen sizes and frame rate?

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

12 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

Related Questions

Simple question about unity 5 rigidbody/gravity 1 Answer

Android velocity slower than editor 0 Answers

Scaling an object causes slow movement (Since Update 5.5.4) 1 Answer

How to move a rigidbody2D at a constant speed without AddForce? 1 Answer

After AddTorque, the resulting angularVelocity depends of the RigidBody's velocity? 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