I’m trying to rotate the wheels of a car whenever you hold left/right. I have it working, but I want to be able to limit how far the rotation goes, something like 30 degrees or so.
using UnityEngine;
using System.Collections;
public class CarController : MonoBehaviour
{
public GameObject[] wheels;
public float turnSpeed;
private float wheelRotation;
void Update ()
{
wheelRotation = Input.GetAxis("Horizontal") * turnSpeed * Time.deltaTime;
if (Input.GetKey("d"))
{
wheels[0].transform.Rotate(0,wheelRotation,0, Space.World);
wheels[1].transform.Rotate(0,wheelRotation,0, Space.World);
wheels[2].transform.Rotate(0,wheelRotation,0, Space.World);
wheels[3].transform.Rotate(0,wheelRotation,0, Space.World);
}
if (Input.GetKey("a"))
{
wheels[0].transform.Rotate(0,wheelRotation,0, Space.World);
wheels[1].transform.Rotate(0,wheelRotation,0, Space.World);
wheels[2].transform.Rotate(0,wheelRotation,0, Space.World);
wheels[3].transform.Rotate(0,wheelRotation,0, Space.World);
}
}
}
I’ve tried Mathf.Clamp on wheelRotation, but all that does is slow down how fast it rotates.
Also, I’d like for when you let go of the button, the wheel goes back to a neutral position. I’m thinking I’ll have to do some sort of Lerp or Slerp in GetKeyUp, because otherwise it’d snap back instantly, which I don’t want. So how would I go about doing this?