- Home /
Tilting A Hover Car, need to lock the rotation
So I have a vehicle(ripped right out of the unity sample file, and a tutorial from youtube (https://www.youtube.com/watch?v=J_IYcjL1mtE) So here's wha
When i turn the vehicle, i want it to tilt into the turn, much like Wipeout. The problem is, whenever you apply the force, it continues to rotate. I tried Mathf.Clamp, but i think im grabbing the wrong things here. Code from project:
using UnityEngine;
using System.Collections;
public class HoverController : MonoBehaviour {
public float speed = 90f;
public float tiltAngle= 0f;
public float tiltForce = .001f;
public float turnSpeed = 5f;
public float hoverForce = 65f;
public float hoverHeight = 3.5f;
private float powerInput;
private float turnInput;
private const float MAX_TILT = .1;
private Rigidbody carRigidbody;
void Awake ()
{
carRigidbody = GetComponent <Rigidbody>();
}
void Update ()
{
powerInput = Input.GetAxis ("Vertical");
turnInput = Input.GetAxis ("Horizontal");
tiltAngle = turnInput * tiltForce;
}
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,0f,Mathf.Clamp(tiltAngle,-MAX_TILT,MAX_TILT));
}
}
Answer by gribbly · Oct 29, 2014 at 08:59 PM
You probably need to add some angular drag to the rigid body. If angular drag is zero, there's no angular friction to counteract the force added by AddRelativeTorque(), and the rigidbody will rotate forever.
For the time being, I added a force that when my turn input = 0, it Lerp's back to zero, but that behavior wasn't right either. I get the feeling I need to give it "angular gravity" if that were a thing