Floating Problem With Rigidbody.Velocity in Unity

Below is the code that I am using to move an object in Unity. The rb.Velocity line makes my object float in in Gameplay mode. If I comment out the line then the object falls just fine.

Could someone explain whats happening here?

public class PlayerController : MonoBehaviour
{

    public float forwardVelocity = 0F;
    public float maxSpeed = 180;
    public float acceleratePerSecond = 8.0F;
    public float rotateSpeed = 3.0F;
    private float yaw = 0.0f;
    private float pitch = 0.0f;
    protected Rigidbody rb;
    float timeZeroToMax = 2.5F;

    // Start is called before the first frame update
    void Start()
    {
        rb = GetComponent<Rigidbody>();
        acceleratePerSecond = maxSpeed / timeZeroToMax;
        forwardVelocity = 0F;

    }

    // Update is called once per frame
    void Update()
    {

            if (Input.GetKey(KeyCode.UpArrow)) //Accelerate The Vehicle
            {
                if (forwardVelocity< maxSpeed)
                {
                forwardVelocity += acceleratePerSecond * Time.deltaTime;

                }

            }


        forwardVelocity = Mathf.Min(forwardVelocity, maxSpeed);


        rb.velocity = transform.forward * forwardVelocity;


        transform.Rotate(0, Input.GetAxis("Mouse X") * rotateSpeed, 0);


        yaw += rotateSpeed * Input.GetAxis("Mouse X");


        transform.eulerAngles = new Vector3(pitch, yaw, 0.0f);
    }

}

since forwardVelocity = 0, rb.velocity = 0

1 Like

Gravity acts (if you don’t change in) in the Y axis of the velocity vector, you’re constantly overriding the gravity by setting it to 0 (assuming your object is not looking up or down) or where ever this vector ends up pointing to.

You need to incorparate the previous fall speed into the new velocity, or stop overriding the velocity and add to it instead of setting it.

Mor elaboration over here would be highly appreciated.

More elaboration of what? @SparrowsNest 's comment is pretty much the whole story. What do you need help understanding?

1 Like

Saying what isn’t clear to you will probably help us explain it better. (;

1 Like

Gravity affects velocity, but on line 40 you are setting velocity directly, thereby erasing the effects of gravity.

1 Like

Ok, so what is the approach that i would use over here?

I tried setting a bool var which checks whether the Object is grounded in the OnCollissionEnter method.

If the object is grounded then only will the code run. but even that doesn’t work.

Or should i be doing something else apart from “rb.velocity = transform.forward”??

Ok guys, one last question though no one bothered to reply to my last post.

Maybe i do not understand vectors fully. From my knowledge a vector multiplied by another vector gives u a result of the by product eg:

v1 (1,2,3) x v2(2,2,2) = v3(2,4,6). I hope i am correct.

but what is the result of this:

v1(0,0,1) * 2; ???

Could someone please explain?

v(x,y,z) * a = v(ax,ay,az)