Files
CarTest/Assets/Scripts/WheelController.cs

99 lines
3.6 KiB
C#
Raw Normal View History

2023-06-11 17:51:10 +02:00
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
2023-06-12 20:36:16 +02:00
using TMPro;
2023-06-11 17:51:10 +02:00
public class WheelController : MonoBehaviour
{
2023-06-12 20:36:16 +02:00
[SerializeField] private WheelCollider frontRightWheelCollider;
[SerializeField] private WheelCollider frontLeftWheelCollider;
[SerializeField] private WheelCollider rearRightWheelCollider;
[SerializeField] private WheelCollider rearLeftWheelCollider;
2023-06-11 17:51:10 +02:00
//wheel meshes
2023-06-12 20:36:16 +02:00
[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;
2023-06-11 17:51:10 +02:00
public float acceleration = 500f;
public float breakingForce = 300f;
2023-06-12 20:36:16 +02:00
[Range(15f,75f)] public float maxTurnAngle;
2023-06-11 17:51:10 +02:00
private float currentAcceleration = 0f;
private float currentBrakeForce = 0f;
private float currentTurnAngle = 0f;
2023-06-12 20:36:16 +02:00
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;
}
2023-06-11 17:51:10 +02:00
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))
2023-06-12 20:36:16 +02:00
ApplyAccelerationToWheel(rearRightWheelCollider, currentAcceleration);
ApplyAccelerationToWheel(rearLeftWheelCollider, currentAcceleration);
2023-06-11 17:51:10 +02:00
//break applies to all four wheels
2023-06-12 20:36:16 +02:00
ApplyBrakeToWheel(frontLeftWheelCollider, currentBrakeForce);
ApplyBrakeToWheel(frontRightWheelCollider, currentBrakeForce);
ApplyBrakeToWheel(rearLeftWheelCollider, currentBrakeForce);
ApplyBrakeToWheel(rearRightWheelCollider, currentBrakeForce);
2023-06-11 17:51:10 +02:00
//steering
2023-06-12 20:36:16 +02:00
currentTurnAngle = maxTurnAngle * Input.GetAxis("Horizontal"); //getting steering input
ApplyStreering(currentTurnAngle);
2023-06-11 17:51:10 +02:00
//update wheel meshes
UpdateWheel(frontLeftWheelCollider, frontLeftWheel);
UpdateWheel(frontRightWheelCollider, frontRightWheel);
UpdateWheel(rearLeftWheelCollider, rearLeftWheel);
UpdateWheel(rearRightWheelCollider, rearRightWheel);
}
2023-06-12 20:36:16 +02:00
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;
}
2023-06-11 17:51:10 +02:00
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;
}
}