I'm following this (https://www.youtube.com/watch?v=kAEpLX3rfms) video on how to make a car in Unity. But my car doesn't lower the steering angle based on the speed of the video. The idea was that you can steer the car at a max of 50 degrees at the slowest and then 10 degrees at the fastest. But it's having no effect on the car. Could someone show me how to fix this?
Code:
public WheelCollider wheelFL;
public WheelCollider wheelFR;
public WheelCollider wheelRL;
public WheelCollider wheelRR;
public Transform wheelFL_Trans;
public Transform wheelFR_Trans;
public Transform wheelRL_Trans;
public Transform wheelRR_Trans;
public float maxSpeed;
public float lowSpeedSteerAngle;
public float highSpeedSteerAngle;
public float Torque;
public float centerOfMassHeight;
private float currentSteerAngle;
private Vector3 com;
private Rigidbody rigidBody;
void Start () {
rigidBody = GetComponent<Rigidbody>();
com = rigidBody.centerOfMass;
com.y = centerOfMassHeight;
rigidBody.centerOfMass = com;
}
void FixedUpdate () {
wheelRL.motorTorque = Torque * Input.GetAxis("Vertical");
wheelRR.motorTorque = Torque * Input.GetAxis("Vertical");
//Here is the part with the issues
float speedFactor = GetComponent<Rigidbody>().velocity.magnitude / maxSpeed;
currentSteerAngle = Mathf.Lerp(highSpeedSteerAngle, lowSpeedSteerAngle, speedFactor) * Input.GetAxis("Horizontal");
wheelFL.steerAngle = currentSteerAngle;
wheelFR.steerAngle = currentSteerAngle;
}
void Update () {
wheelFL_Trans.Rotate(0, 0, wheelFL.rpm / 60 * 360 * Time.deltaTime);
wheelFR_Trans.Rotate(0, 0, wheelFR.rpm / 60 * 360 * Time.deltaTime);
wheelRL_Trans.Rotate(0, 0, wheelRL.rpm / 60 * 360 * Time.deltaTime);
wheelRR_Trans.Rotate(0, 0, wheelRR.rpm / 60 * 360 * Time.deltaTime);
Vector3 wheelSteerAngle = wheelFL_Trans.localEulerAngles;
wheelSteerAngle.y = currentSteerAngle + 90;
wheelFL_Trans.localEulerAngles = wheelSteerAngle;
wheelFR_Trans.localEulerAngles = wheelSteerAngle;
if(Input.GetKeyDown(KeyCode.P)){
Vector3 resetPosition = new Vector3(transform.position.x, 1.5f, transform.position.z);
Quaternion resetRotation = Quaternion.Euler(0f, 0f, 0f);
//Reseting Velocity
GetComponent<Rigidbody>().velocity = new Vector3(0f, 0f, 0f);
//Reseting Rotation
transform.rotation = resetRotation;
//Reseting Position
transform.position = resetPosition;
//Reseting Motor Torque and Steering Angle
wheelFL.motorTorque = 0f;
wheelFR.motorTorque = 0f;
wheelRL.motorTorque = 0f;
wheelRR.motorTorque = 0f;
wheelFL.steerAngle = 0f;
wheelFR.steerAngle = 0f;
wheelRL.steerAngle = 0f;
wheelRR.steerAngle = 0f;
}
}