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);
}
}
}