How to add simple spacebar jump command?

Hi all!

I’m still learning to use Unity and scripting psysics on it. I have this code allready where i want to add spacebar jump so that my hovercar can jump across the track.

What is the simple way to define code that work with that “public float jumpPower”?

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class PlayerControls : MonoBehaviour
{

    public float thrustSpeed;
    public float turnSpeed;
    public float hoverPower;
    public float hoverHeight;
    public float jumpPower = 1;

    private float thrustInput;
    private float turnInput;
    private Rigidbody shipRigidBody;

    // Use this for initialization
    void Start()
    {
        shipRigidBody = GetComponent<Rigidbody>();
    }

    // Update is called once per frame
    void Update()
    {
        thrustInput = Input.GetAxis("Vertical");
        turnInput = Input.GetAxis("Horizontal");
    }
   
     

void FixedUpdate()
    {
        // Turning the ship
        shipRigidBody.AddRelativeTorque(0f, turnInput * turnSpeed, 0f);

        // Moving the ship
        shipRigidBody.AddRelativeForce(0f, 0f, thrustInput * thrustSpeed);

        // Hovering
        Ray ray = new Ray(transform.position, -transform.up);
        RaycastHit hit;
        if (Physics.Raycast(ray, out hit, hoverHeight))
        {
            float proportionalHeight = (hoverHeight - hit.distance) / hoverHeight;
            Vector3 appliedHoverForce = Vector3.up * proportionalHeight * hoverPower;
            shipRigidBody.AddForce(appliedHoverForce, ForceMode.Acceleration);
        }
    }
}

I didn’t get the ‘Hovering’ part in your code, I’m assuming it’s used to make the car hover all the time above the track, and you apply force since you want to make it seem smooth?

With your turning and force, I would add Time.fixedDeltaTime in order to consider the framerate calculation.

You can directly manipulate rigidbody velocity for the jump effect, although using a force towards up, would also benefit.