RigidBody Velocity Problems

I have a script:

public float CharacterWalkSpeed;



if (Input.GetKey(KeyCode.UpArrow) || Input.GetKey(KeyCode.W))
        {
            if (rb.velocity<CharacterWalkSpeed)
            {
rb.AddForce(transform.forward * CharacterWalkSpeed, ForceMode.Impulse);
            }

and it’s assigned to a cube. What I want to happen when the up arrow or the w key is pressed, for there to be a specific amount of force (CharacterWalkSpeed) to be applied to the cube. However, if the amount of velocity is greater than my original speed, the force will stop being applied. The problem though is that the if (rb.velocity<CharacterWalkSpeed) does not work. Once again, I’m trying to see if the objects velocity is less than the CharacterWalkSpeed.

isn’t rb.velocity a vector3?

I believe so, so how could I rephrase the if statement?

I’m not sure but I think you will need to convert the vector 3 to floats

This is answered in another thread on the same/similar topic by the OP:

if(rb.velocity.magnitude) /* etc */
1 Like