Why is this script slowing down the gravity of a rigidbody?

So the problem is that when I have the first 3 lines of code in there, the gravity is cut down most of the way and my character falls slowly, also the jump part of the code stops working. When I jump it moves up about .1 on the y axis, then starts falling slowly back to the ground. But when I have line 1-3 commented out the jump script works.

Is there something I can do to line 3 so I can keep line 1-2 the same and for the jump to work?

(Speed is set to 10.0F)

        Vector3 forwardVel = transform.forward * speed * Input.GetAxis ("Vertical");
         Vector3 horizontalVel = transform.right * speed * Input.GetAxis ("Horizontal");
         GetComponent<Rigidbody>().velocity = forwardVel + horizontalVel;
//Jump
    if (rb.velocity.y == 0 && Input.GetButtonDown("Jump"))
         {

                 rb.AddForce(new Vector3(0, 5, 0), ForceMode.Impulse);   
         }

Hm… maybe it’s gravity? Did you change the parameter of gravity? You can change it from Editor or script. Check Physics.gravity value.

The physics system adds the gravity force to the Rigidbody velocity. You’re completely overwriting the Rigidbody velocity here:

GetComponent<Rigidbody>().velocity = forwardVel + horizontalVel;

Later you do this:

rb.AddForce(new Vector3(0, 5, 0), ForceMode.Impulse);

Do you know what an impulse force is? It adds the force you specify onto the velocity of the Rigidbody immediately i.e. velocity += force.

This is what you should be doing above instead of overwriting the Rigidbody velocity. Either that or you can simply add your ‘forwardVel + horizontalVel’ to the velocity.

So apply impulse force for both horizontal and vertical forces?

Whelp, I tested this, and now I have another problem. It applys it globally, but I need it to apply it locally because my player rotates. How can I do the impulse force and apply it locally?

If rotating is problem, consider constraints of rigidbody.

Great that you test “this” but unfortunatley we can’t see your code. You obviously apply an impulse force in local-space by passing it a local-space vector just like you did when you set a local-space vector for the velocity. Nothing changes.

Note that you’re using Vector3 which might be convenient but 2D requires a Vector2. It performs a conversion for you by simply stripped the Z away but note that this can cause subtle problems if you’re not using vectors aligned to the XY plane.