Files
CarTest/Assets/Scripts/WheelController.cs

87 lines
2.7 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-14 14:35:32 +02:00
using UnityEngine.InputSystem;
2023-06-11 17:51:10 +02:00
public class WheelController : MonoBehaviour
{
2023-06-14 14:35:32 +02:00
[SerializeField] private Wheel frontRightWheel;
[SerializeField] private Wheel frontLeftWheel;
[SerializeField] private Wheel rearRightWheel;
[SerializeField] private Wheel rearLeftWheel;
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-14 14:35:32 +02:00
private DebugInfo debugInfo;
//inputs
private InputMaster controls;
private InputAction movement;
private InputAction turn;
private InputAction brake;
private void Awake() {
debugInfo = FindObjectOfType<DebugInfo>();
controls = new InputMaster();
2023-06-12 20:36:16 +02:00
}
2023-06-14 16:27:21 +02:00
private void OnEnable() {
movement = controls.Car.Movement;
movement.Enable();
turn = controls.Car.Turn;
turn.Enable();
brake = controls.Car.Brake;
brake.Enable();
}
private void OnDisable() {
movement.Disable();
turn.Disable();
brake.Disable();
}
2023-06-14 14:35:32 +02:00
private void Update() {
debugInfo.setRpmLabels(frontLeftWheel.GetWheelRPM(), frontRightWheel.GetWheelRPM(), rearLeftWheel.GetWheelRPM(), rearRightWheel.GetWheelRPM());
2023-06-12 20:36:16 +02:00
}
2023-06-11 17:51:10 +02:00
private void FixedUpdate() {
2023-06-14 14:35:32 +02:00
//TODO: Update input info to new input system
2023-06-11 17:51:10 +02:00
//forward reverse input
2023-06-14 16:27:21 +02:00
//Debug.Log("movement value: " + movement.ReadValue<float>());
currentAcceleration = acceleration * movement.ReadValue<float>();
2023-06-14 14:35:32 +02:00
2023-06-11 17:51:10 +02:00
//breaking input
2023-06-14 16:27:21 +02:00
//Debug.Log("brake: " + brake.ReadValue<float>());
currentBrakeForce = breakingForce * brake.ReadValue<float>();
2023-06-11 17:51:10 +02:00
//apply acceleration to front wheels| (this determines which wheel drive is the car (fwd, awd, rwd))
2023-06-14 14:35:32 +02:00
rearRightWheel.ApplyAcceleration(currentAcceleration);
rearLeftWheel.ApplyAcceleration(currentAcceleration);
2023-06-11 17:51:10 +02:00
//break applies to all four wheels
2023-06-14 14:35:32 +02:00
frontLeftWheel.ApplyBrakeForce(currentBrakeForce);
frontRightWheel.ApplyBrakeForce(currentBrakeForce);
rearLeftWheel.ApplyBrakeForce(currentBrakeForce);
rearRightWheel.ApplyBrakeForce(currentBrakeForce);
2023-06-11 17:51:10 +02:00
//steering
2023-06-14 16:27:21 +02:00
currentTurnAngle = maxTurnAngle * turn.ReadValue<float>(); //getting steering input
2023-06-14 14:35:32 +02:00
frontLeftWheel.ApplySteerAngle(currentTurnAngle);
frontRightWheel.ApplySteerAngle(currentTurnAngle);
2023-06-11 17:51:10 +02:00
//update wheel meshes
2023-06-14 14:35:32 +02:00
frontLeftWheel.UpdateWheel();
frontRightWheel.UpdateWheel();
rearLeftWheel.UpdateWheel();
rearRightWheel.UpdateWheel();
2023-06-11 17:51:10 +02:00
}
}