- Home /
Current speed of an object?
I'm scripting the animation for my character.
How can I check the speed of the character?
My code:
animation.Stop(); animation.Play("Idle");
function Update () {
if (INSERT_SPEED_VARIABLE_HERE) > 0.1) { animation.CrossFade("WalkCycle"); } }
Thanks
Answer by boribhi · Aug 03, 2010 at 01:33 AM
if your character has a rigidbody, try:
var vel = rigidbody.velocity; //to get a Vector3 representation of the velocity
or
var vel = rigidbody.velocity; //to get a Vector3 representation of the velocity
speed = vel.magnitude; // to get magnitude
How are you changing the position of your character? Are you adding force, or just setting the transform.position each frame?
Why not trying to add a rigidbody? if you have not to add one, tell us what your character consists of.
definitely don't start using the physics engine just to know how fast your character is moving :) but if you're already using it then this is the way to go.
I'm applying this to the vehicle I'm making, but it still gives me a positive velocity when I'm driving in reverse. How do I fix this?
Answer by trapazza · Apr 02, 2013 at 02:27 PM
In case you only have their transforms:
speed = total distance traveled / total time taken
or something like:
float speed = (transform.position - this.mLastPosition).magnitude / elapsedTime;
this.mLastPosition = transform.position;
Answer by Caps · Dec 02, 2010 at 07:23 PM
if youre using character controller:
http://unity3d.com/support/documentation/ScriptReference/CharacterController-velocity.html
Answer by NSO · Apr 11, 2015 at 06:52 PM
var vel = rigidbody.velocity; This will get your current velocity in x, y and z not your current speed. If you are traveling with the current velocity (60, 0, 0) it is correct and can be used but NOT with the velocity (60, 60, 0). I say how to get the speed of a rigidbody when i solved the problem cant find any clues on the big net.
or, if you want just the speed on a plane (for example, ground speed) use Pythagoras to deter$$anonymous$$e the magnitude across the x-z axes
Answer by amitDklein · Sep 15, 2020 at 08:17 AM
You can use Transform.hasChanged
if (!this.transform.hasChanged)
{
print("Player is not moving");
}
transform.hasChanged = false;
So setting, for instance, transform.position will always set hasChanged on the transform, regardless of there being any actual change.
So even if the flag is true, it does not mean the object is moving.
Other examples: object rotates, object is scaled, parent is changed
For this example, and many others, this is the most simple solution.
Any way, if the object rotates, scaled, or parent is changed the object will be kind of moving...
$$anonymous$$ost of the places I know they check for if object not moving will be best using this method.
Your answer
Follow this Question
Related Questions
JavaScript and Ajax second addition Tutorial 11 case problem 2 0 Answers
Desactive a object 1 Answer
Animation play on input, else play idle animation. 2 Answers
Walking animation problem. 0 Answers
Enemy Attack Animation 1 Answer