- Home /
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^^^^^^^^
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).
I placed it in a var and the var while in game did not change from 0 even though i was moving
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.
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.
WOW... thanks so much i was really stumpted!! it works =)
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
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
Follow this Question
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