My car doesn't reverse, I've tried adding an else if for when the velocity is zero and s is being held down that motorTorque = -torque but this does nothing. Here is my code:
public class CarMovement : MonoBehaviour
{
public GameObject backLeftWheel, backRightWheel, frontLeftWheel, frontRightWheel;
WheelCollider blWheel, brWheel,flWheel,frWheel;
public Rigidbody car;
public float carVelocity;
// Start is called before the first frame update
public float torque, brakeTorque, maxSpeed, highSpeedSteeringAngle, lowSpeedSteeringAngle;
public Vector3 resetPosition;
public Quaternion resetRotation;
void Start()
{
car.centerOfMass = new Vector3 (0.0f, -0.75f, .35f);
blWheel = backLeftWheel.GetComponent ();
brWheel = backRightWheel.GetComponent ();
flWheel = frontLeftWheel.GetComponent();
frWheel = frontRightWheel.GetComponent ();
}
// Update is called once per frame
void Update()
{
carVelocity = this.gameObject.GetComponent ().velocity.magnitude;
if (Input.GetAxis ("Vertical") > 0 && carVelocity < maxSpeed) {
blWheel.motorTorque = torque;
brWheel.motorTorque = torque;
} else if (Input.GetAxis ("Vertical") < 0 && carVelocity > 0) {
blWheel.brakeTorque = brakeTorque;
brWheel.brakeTorque = brakeTorque;
flWheel.brakeTorque = brakeTorque;
frWheel.brakeTorque = brakeTorque;
}
else
{
blWheel.brakeTorque = 0;
brWheel.brakeTorque = 0;
flWheel.brakeTorque = 0;
frWheel.brakeTorque = 0;
blWheel.motorTorque = 0;
brWheel.motorTorque = 0;
}
if (Input.GetAxis ("Horizontal") != 0) {
flWheel.steerAngle = lowSpeedSteeringAngle*Input.GetAxis ("Horizontal");
frWheel.steerAngle = lowSpeedSteeringAngle*Input.GetAxis ("Horizontal");
}
if (this.transform.position.y < -1f) {
resetCar ();
}
}
↧