Sdrive
Text : FourWheelDrive.txt
using UnityEngine; public class FourWheelDriveCarController : MonoBehaviour { public Transform[] wheelMeshes; public WheelCollider[] wheelColliders; public float maxTorque = 500f; public float maxSteeringAngle = 30f; public float brakeTorque = 2000f; public float downforce = 100f; private Rigidbody rb; private void Start() { rb = GetComponent
(); // Set wheel colliders to use smooth contact. foreach (WheelCollider wc in wheelColliders) { wc.ConfigureVehicleSubsteps(5f, 12, 15); } } private void FixedUpdate() { // Get inputs. float steering = Input.GetAxis("Horizontal"); float acceleration = Input.GetAxis("Vertical"); bool isBraking = Input.GetButton("Jump"); // Calculate torque and steering angle. float torque = acceleration * maxTorque; float steeringAngle = steering * maxSteeringAngle; // Apply torque and steering to each wheel. foreach (WheelCollider wc in wheelColliders) { wc.motorTorque = torque; wc.steerAngle = steeringAngle; // Apply braking if the jump button is pressed. wc.brakeTorque = isBraking ? brakeTorque : 0; // Rotate the wheel mesh to match the collider. Quaternion quat; Vector3 position; wc.GetWorldPose(out position, out quat); foreach (Transform wt in wheelMeshes) { wt.rotation = quat; } } // Apply downforce. rb.AddForce(-transform.up * downforce * rb.velocity.magnitude); } }
File Updated
Download
Delete
Rename
Start