Description
I currently have a hovercar working with physics and the script I created is based off of the hovercar tutorial made by Unity themselves. The problem I am having is that the I am finding the acceleration of the hovercar too high and its reaching its top speed quickly. Is there any way I can implement slower acceleration but maintaining control over the vehicle with the current setup I have below?
N.B. I also want this because I want the hovercars to have different attributes like top speed, acceleration, handling, etc.
Info
Tutorial: Unity Connect (script can be found under video)
The scripts and rigidbody settings are exactly the same except for some few things:
Rigidbody:
- Use Gravity is off (I’ll explain later)
- Interpolate is set to Interpolate (since for some reason my hover car stutters without this)
Script:
Check the code comments for the changes (fixed update part only is shown since physics are only done there)
void FixedUpdate()
{
// I added my own fake gravity here
Vector3 appliedGravityForce = -transform.up * 9.81f;
carRigidbody.AddForce(appliedGravityForce, ForceMode.Acceleration);
Ray ray = new Ray(carFlyer.position, -transform.up); // used a child object in front of the car as the position to make the detection more accurate
RaycastHit hit;
if (Physics.Raycast(ray, out hit, hoverHeight * 1.5f)) // made detection be a bit broader
{
float proportionalHeight = (hoverHeight - hit.distance) / hoverHeight;
Vector3 appliedHoverForce = transform.up * proportionalHeight * hoverForce;
carRigidbody.AddForce(appliedHoverForce, ForceMode.Acceleration);
Quaternion targetRotation = Quaternion.FromToRotation(transform.up, hit.normal) * carRigidbody.rotation;
carRigidbody.rotation = Quaternion.Slerp(carRigidbody.rotation, targetRotation, Time.fixedDeltaTime * 3);
// I adjusted the car's angles to the normal of the ground under it
Debug.DrawRay(ray.origin, -transform.up, Color.cyan); // just some debug stuff
}
carRigidbody.AddRelativeForce(0f, 0f, powerInput * speed);
carRigidbody.AddRelativeTorque(0f, turnInput * turnSpeed, 0f);
}