87 lines
2.7 KiB
C#
87 lines
2.7 KiB
C#
using System.Collections;
|
|
using System.Collections.Generic;
|
|
using UnityEngine;
|
|
using UnityEngine.InputSystem;
|
|
|
|
public class WheelController : MonoBehaviour
|
|
{
|
|
[SerializeField] private Wheel frontRightWheel;
|
|
[SerializeField] private Wheel frontLeftWheel;
|
|
[SerializeField] private Wheel rearRightWheel;
|
|
[SerializeField] private Wheel rearLeftWheel;
|
|
|
|
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 DebugInfo debugInfo;
|
|
|
|
//inputs
|
|
private InputMaster controls;
|
|
private InputAction movement;
|
|
private InputAction turn;
|
|
private InputAction brake;
|
|
|
|
private void Awake() {
|
|
debugInfo = FindObjectOfType<DebugInfo>();
|
|
controls = new InputMaster();
|
|
}
|
|
|
|
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();
|
|
}
|
|
|
|
private void Update() {
|
|
debugInfo.setRpmLabels(frontLeftWheel.GetWheelRPM(), frontRightWheel.GetWheelRPM(), rearLeftWheel.GetWheelRPM(), rearRightWheel.GetWheelRPM());
|
|
}
|
|
|
|
private void FixedUpdate() {
|
|
//TODO: Update input info to new input system
|
|
|
|
//forward reverse input
|
|
//Debug.Log("movement value: " + movement.ReadValue<float>());
|
|
currentAcceleration = acceleration * movement.ReadValue<float>();
|
|
|
|
//breaking input
|
|
//Debug.Log("brake: " + brake.ReadValue<float>());
|
|
currentBrakeForce = breakingForce * brake.ReadValue<float>();
|
|
|
|
//apply acceleration to front wheels| (this determines which wheel drive is the car (fwd, awd, rwd))
|
|
rearRightWheel.ApplyAcceleration(currentAcceleration);
|
|
rearLeftWheel.ApplyAcceleration(currentAcceleration);
|
|
|
|
//break applies to all four wheels
|
|
frontLeftWheel.ApplyBrakeForce(currentBrakeForce);
|
|
frontRightWheel.ApplyBrakeForce(currentBrakeForce);
|
|
rearLeftWheel.ApplyBrakeForce(currentBrakeForce);
|
|
rearRightWheel.ApplyBrakeForce(currentBrakeForce);
|
|
|
|
//steering
|
|
currentTurnAngle = maxTurnAngle * turn.ReadValue<float>(); //getting steering input
|
|
frontLeftWheel.ApplySteerAngle(currentTurnAngle);
|
|
frontRightWheel.ApplySteerAngle(currentTurnAngle);
|
|
|
|
//update wheel meshes
|
|
frontLeftWheel.UpdateWheel();
|
|
frontRightWheel.UpdateWheel();
|
|
rearLeftWheel.UpdateWheel();
|
|
rearRightWheel.UpdateWheel();
|
|
}
|
|
} |