How do I maintain momentum while jumping with a rigidbody


So this is my Rocket League spinoff summer project.
My car just stops and jumps. I want it to be able to jump straight up when not moving and keep momentum when it is moving and in the air. This is a newb way to write this code. Let me know if you have any suggestions. Thanks!

using UnityEngine;
using System.Collections;


public class Jump : MonoBehaviour
{
    public Rigidbody truck;

    void Start()
    {
        truck = GetComponent<Rigidbody>();
    }
    void FixedUpdate()
    {
        if (Input.GetKeyDown(KeyCode.B))
        {
            truck = GetComponent<Rigidbody>();
            truck.velocity = new Vector3(0, 7, 0);
        }
        else if (Input.GetKeyDown(KeyCode.LeftControl)) { 
            truck.velocity = new Vector3(0, 3, 4);
             }
    }

        }

Let me know if there is something I am missing. I am on 5.3.5f1,

Duh? you’re cancelling out the x and z velocities. So read existing velocity and only modify Y, once when you jump :slight_smile:

I am a newbie to this… And what about rotation of the car? Like once “t” is hit, the car can do barrel rolls and rotate/frontflips and backflips

Thanks for your help btw!

Are you using wheelcolliders? They would make life much easier for the driving behavior at least (there is an arcade’ish script in my sig if you need)… as for the jump boost

   if(Input.GetKeyDown(KeyCode.B))
        rigidbody.velocity += new Vector3(0,10,0);

I have solved this. However still need a solution to rotate my car in the air on the x, y, z axis.

I would use AddRelativeTorque for that.

Check docs for examples