Rigidbody.velocity.magnitude only returns value of the Y axis

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

Have you tried logging the value of the velocity? And how are you moving the rigidbody? If for example you move it laterally by adjusting the transform's position directly, but jump by applying an upwards force or adjusting the velocity directly, then I might expect something like what you're describing.

@Bonfire Boy I am moving the rigidbody using MovePosition. So is there a way I can get the velocity using this method?

have you tried addforce? it should work

@rhbrr5hrfgdfgw I've used it before and might be able to implement it later. I can't do it right now but if I get it working later, I'll come back with a good comment. Else I'll just ask for more help. :P

1 Answer

1

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.

I have been trying to switch to AddForce but I can't. I added moveDirection(Vector3) = new Vector3(Input.GetAxisRaw("Horizontal"), 0, Input.GetAxisRaw("Vertical")); rig.AddRelativeForce(moveDirection * movementSpeed * Time.deltaTime); to FixedUpdate but the player just stands there. It only moves when I jump and then the player slows down and is uncontrollable. What could cause this?

Immobility on the ground could be the result of friction, then. Physics materials can let you modify the default PhysX values for friction interaction between objects, though working with various shapes and sizes can have unexpected results. For instance, cubes would have a very large surface contact area potential, so they often exhibit much greater friction.

@Eno Khaon I am using a capsule collider and disabled everything that had to do with physics materials and it does no difference. I even tried to apply a zero friction material and that also did nothing.