Hello there guys.
I am relatively new to Unity and I just started following some of the Unity Youtube Tutorials. So I followed this tutorial about creating a simple Hover Car and then I started playing around with the values. I noticed that when I make the speed variable higher, the car slowly gains height when driving.
Here is the code:
using UnityEngine;
using System.Collections;
public class HoverMotor : MonoBehaviour {
public float speed = 90f;
public float turnspeed = 5f;
public float hoverForce = 65f;
public float hoverHeight = 2f;
private float powerInput;
private float turnInput;
private Rigidbody carRigidbody;
void Awake () {
carRigidbody = GetComponent<Rigidbody>();
}
// Update is called once per frame
void Update ()
{
powerInput = Input.GetAxis("Vertical");
turnInput = Input.GetAxis("Horizontal");
}
void FixedUpdate()
{
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 * hoverForce;
carRigidbody.AddForce(appliedHoverForce, ForceMode.Acceleration);
}
carRigidbody.AddRelativeForce(0f, 0f, powerInput * speed);
carRigidbody.AddRelativeTorque(0f, turnInput * turnspeed, 0f);
}
}
I am thankfull for any help and I hope I can learn something today ![]()