- Home /
I have an animated character, need to find current speed
I have a character that is moving along a NavMesh, I can't figure out how to get its current speed as a float.
I've tried using the below code, but it always return zero.
Also, since my character has isKinematic = true...is this what is preventing me from getting velocity?
public class Walk : MonoBehaviour {
private Rigidbody rigidBody;
void Start() {
rigidBody = gameObject.GetComponent<Rigidbody>();
}
void Update(){
var currentSpeed = rigidBody.velocity.magnitude;
Debug.Log("currentSpeed:" + currentSpeed );
}
}
It's on a navmesh.. is the character a Nav$$anonymous$$eshAgent? If it is, you can access its velocity by getting the Nav$$anonymous$$eshAgent component, then accessing that variable's nav$$anonymous$$eshAgent.velocity
This is exactly what I was looking for. This helped me out a lot! Thanks.
Nav$$anonymous$$eshAgent .velocity.magnitude
Remember to get the absolut value of float, because it can return an negative velocity, so use
$$anonymous$$athf.Abs(Nav$$anonymous$$eshAgent. velocity.magnitude);
Your answer

Follow this Question
Related Questions
How to Rotate 2d Object with constant speed ? 1 Answer
Rigidbody2D.velocity is not working. 0 Answers
How do I add forward velocity to a Rigidbody2D? 2 Answers
Rigid Body Velocity max 1 Answer