Question by
kyran5 · Mar 31, 2021 at 02:23 AM ·
c#movement scriptwheelcollidercar physicsreverse
How to get car to reverse using motorTorque/WheelColliders?
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<WheelCollider> ();
brWheel = backRightWheel.GetComponent<WheelCollider> ();
flWheel = frontLeftWheel.GetComponent<WheelCollider>();
frWheel = frontRightWheel.GetComponent<WheelCollider> ();
}
// Update is called once per frame
void Update()
{
carVelocity = this.gameObject.GetComponent<Rigidbody> ().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 ();
}
}
Comment
Your answer
Follow this Question
Related Questions
How to rotate wheel colliders on the Y axis since they can't be rotated in the editor? 0 Answers
animator and script issue 0 Answers
Moving moving Object with left/right arrow keys in a circular direction 3 Answers
Why isn't my player moving down? 0 Answers
Navmesh Agent isStopped not working, can't get navemesh agent to stop. 1 Answer