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
4
Question by BrettRiet · Apr 28, 2011 at 03:18 PM · rigidbodyvariablevelocityspeed

Calculating speed of rigidbody?

I have a car and it's a rigidbody with a force acting on it. How would i make a "public var" showing the speed so i can play in small screen and see the speed on the side bar?

var speed = rigidbody.velocity.magnitude;

didn't work like that^^^^^^^

var speed = rigidbody.velocity;

or like that^^^^^^^^

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

2 Replies

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

Answer by flaviusxvii · Apr 28, 2011 at 03:26 PM

var speed = rigidbody.velocity.magnitude;

Why didn't this "work"? How do you quantify success here? The magnitude of the velocity is what you need, it'll return the velocity of the object in meters per second (assuming you're using the spatial units in Unity as meters, which you should).

Comment
Add comment · Show 8 · 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 BrettRiet · Apr 28, 2011 at 03:29 PM 0
Share

I placed it in a var and the var while in game did not change from 0 even though i was moving

avatar image Joshua · Apr 28, 2011 at 03:30 PM 1
Share

Yeah, it should definitely work. Do make sure you declare the var on top of your function and keep it public so it'll show up in the editor, like you said you wanted. Then put speed = rigidbody.velocity.magnitude; in your update function. And have Time.timeScale set to 1.

avatar image Joshua · Apr 28, 2011 at 03:31 PM 0
Share

Brett, you must on top of your function put var speed : float; and then in your update function place speed = rigidbody.velocity.magnitude; your problem is that you cannot constantly change a variable if it's only set once at the beginning.

avatar image BrettRiet · Apr 28, 2011 at 03:33 PM 0
Share

WOW... thanks so much i was really stumpted!! it works =)

avatar image Hazemdido · Mar 21, 2013 at 12:59 PM 0
Share

Thanks very much :) :) :*

Show more comments
avatar image
1

Answer by umairwasim · Jun 07, 2016 at 07:30 AM

It's better to use FixedUpdate rather than simple Update whenever dealing with physics (rigidbody) objects, that in your case is a car. If you want to have a detailed idea of what's the difference between them, please visit the following link: https://unity3d.com/learn/tutorials/topics/scripting/update-and-fixedupdate

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 Hogge · Mar 21, 2017 at 07:28 PM 0
Share

You're right. Problem was resolved when I moved that stuff to update, but I also had to delete the "else"-statement. So now I'm trying to figure out how to do it without an else-statement.

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

6 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Kinematic how to know rigidbody velocity vector? 1 Answer

Updating Velocity for a Rigidbody Collision 1 Answer

Setting the max velocity of a rigidbody 2 Answers

Need help with scripting (problem related to rigidbody/velocity) 0 Answers

Velocity powered rigidbody on a moving platform without parenting. 3 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