using System.Collections; using System.Collections.Generic; using UnityEngine; using TMPro; public class WheelController : MonoBehaviour { [SerializeField] private WheelCollider frontRightWheelCollider; [SerializeField] private WheelCollider frontLeftWheelCollider; [SerializeField] private WheelCollider rearRightWheelCollider; [SerializeField] private WheelCollider rearLeftWheelCollider; //wheel meshes [SerializeField] private Transform frontRightWheel; [SerializeField] private Transform frontLeftWheel; [SerializeField] private Transform rearRightWheel; [SerializeField] private Transform rearLeftWheel; [SerializeField] private TMP_Text flRpmLabel; [SerializeField] private TMP_Text frRpmLabel; [SerializeField] private TMP_Text rlRpmLabel; [SerializeField] private TMP_Text rrRpmLabel; public float acceleration = 500f; public float breakingForce = 300f; [Range(15f,75f)] public float maxTurnAngle; private float currentAcceleration = 0f; private float currentBrakeForce = 0f; private float currentTurnAngle = 0f; private void Update() { flRpmLabel.text = getWheelRPM(frontLeftWheelCollider).ToString(); frRpmLabel.text = getWheelRPM(frontRightWheelCollider).ToString(); rlRpmLabel.text = getWheelRPM(rearLeftWheelCollider).ToString(); rrRpmLabel.text = getWheelRPM(rearRightWheelCollider).ToString(); } private float getWheelRPM(WheelCollider wheel){ return wheel.rpm; } private void FixedUpdate() { //forward reverse input currentAcceleration = acceleration * Input.GetAxis("Vertical"); //breaking input if(Input.GetKey(KeyCode.Space)){ currentBrakeForce = breakingForce; }else{ currentBrakeForce = 0f; } //apply acceleration to front wheels| (this determines which wheel drive is the car (fwd, awd, rwd)) ApplyAccelerationToWheel(rearRightWheelCollider, currentAcceleration); ApplyAccelerationToWheel(rearLeftWheelCollider, currentAcceleration); //break applies to all four wheels ApplyBrakeToWheel(frontLeftWheelCollider, currentBrakeForce); ApplyBrakeToWheel(frontRightWheelCollider, currentBrakeForce); ApplyBrakeToWheel(rearLeftWheelCollider, currentBrakeForce); ApplyBrakeToWheel(rearRightWheelCollider, currentBrakeForce); //steering currentTurnAngle = maxTurnAngle * Input.GetAxis("Horizontal"); //getting steering input ApplyStreering(currentTurnAngle); //update wheel meshes UpdateWheel(frontLeftWheelCollider, frontLeftWheel); UpdateWheel(frontRightWheelCollider, frontRightWheel); UpdateWheel(rearLeftWheelCollider, rearLeftWheel); UpdateWheel(rearRightWheelCollider, rearRightWheel); } private void ApplyStreering(float streeringAngle){ frontLeftWheelCollider.steerAngle = streeringAngle; frontRightWheelCollider.steerAngle = streeringAngle; } private void ApplyBrakeToWheel(WheelCollider wheel, float breakForce){ wheel.brakeTorque = breakForce; } private void ApplyAccelerationToWheel(WheelCollider wheel, float acceleration){ wheel.motorTorque = acceleration; } private void UpdateWheel(WheelCollider collider, Transform wheel){ //get collider state Vector3 position; Quaternion rotation; collider.GetWorldPose(out position, out rotation); //puts it into position and rotation //set wheel state wheel.position = position; wheel.rotation = rotation; } }