Hi
While trying to get animator on my playing working I noticed that rigidbody.velocity.magnitude only returns a proper number when I am jumping. When I am moving it just gives me numbers like “1.66893E-05” and if I have been taught correct, that means 0. I’ve searched for how I can get the velocity of the rigidbody and all result in rigidbody.velocity.magnitude but that is not working. Am I doing something wrong here?
I am not going to show any code either because I don’t know what code I could show.
Thanks in advance
Taking a note from the comments, if you’re using rigidbody.MovePosition() to move around, you’re not applying any change to your velocity. Therefore, while your velocity IS zero, your rate of movement simulating velocity would be the speed at which you’re directly moving the object.
For example, if you’re using something like:
//C#
Rigidbody rb;
public float speed;
void Start()
{
rb = GetComponent<Rigidbody>();
}
void FixedUpdate()
{
rb.MovePosition((transform.position + Vector3.right * speed) * Time.deltaTime);
}
as your means of movement (without information, I’m speculating a bit), this would be moving on the X axis at a rate of “speed” units per second.
This would not be counted as velocity, but your distance moved per second would be exactly as far as you tell it to be.
As @rhbrr5hrfgdfgw suggested, it would probably be preferable to use AddForce() instead to control rigidbody movement, as that actually applies motion through the object’s velocity instead.
//C#
void FixedUpdate()
{
// Based on object's mass. The higher the mass, the less impact the force has.
rb.AddForce(Vector3.right * speed);
// Ignoring object's mass. Anything of any weight accelerates at the speed listed.
//rb.AddForce(Vector3.right * speed, ForceMode.Acceleration);
}
It also requires less extraneous code.