new input system, wheel in Wheel class
This commit is contained in:
@@ -1,25 +1,14 @@
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
using TMPro;
|
||||
using UnityEngine.InputSystem;
|
||||
|
||||
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;
|
||||
[SerializeField] private Wheel frontRightWheel;
|
||||
[SerializeField] private Wheel frontLeftWheel;
|
||||
[SerializeField] private Wheel rearRightWheel;
|
||||
[SerializeField] private Wheel rearLeftWheel;
|
||||
|
||||
public float acceleration = 500f;
|
||||
public float breakingForce = 300f;
|
||||
@@ -29,71 +18,56 @@ public class WheelController : MonoBehaviour
|
||||
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 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 float getWheelRPM(WheelCollider wheel){
|
||||
return wheel.rpm;
|
||||
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
|
||||
currentAcceleration = acceleration * Input.GetAxis("Vertical");
|
||||
//currentAcceleration = acceleration * Input.GetAxis("Vertical");
|
||||
|
||||
|
||||
//breaking input
|
||||
if(Input.GetKey(KeyCode.Space)){
|
||||
/*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);
|
||||
rearRightWheel.ApplyAcceleration(currentAcceleration);
|
||||
rearLeftWheel.ApplyAcceleration(currentAcceleration);
|
||||
|
||||
//break applies to all four wheels
|
||||
ApplyBrakeToWheel(frontLeftWheelCollider, currentBrakeForce);
|
||||
ApplyBrakeToWheel(frontRightWheelCollider, currentBrakeForce);
|
||||
ApplyBrakeToWheel(rearLeftWheelCollider, currentBrakeForce);
|
||||
ApplyBrakeToWheel(rearRightWheelCollider, currentBrakeForce);
|
||||
frontLeftWheel.ApplyBrakeForce(currentBrakeForce);
|
||||
frontRightWheel.ApplyBrakeForce(currentBrakeForce);
|
||||
rearLeftWheel.ApplyBrakeForce(currentBrakeForce);
|
||||
rearRightWheel.ApplyBrakeForce(currentBrakeForce);
|
||||
|
||||
//steering
|
||||
currentTurnAngle = maxTurnAngle * Input.GetAxis("Horizontal"); //getting steering input
|
||||
ApplyStreering(currentTurnAngle);
|
||||
//currentTurnAngle = maxTurnAngle * Input.GetAxis("Horizontal"); //getting steering input
|
||||
frontLeftWheel.ApplySteerAngle(currentTurnAngle);
|
||||
frontRightWheel.ApplySteerAngle(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;
|
||||
frontLeftWheel.UpdateWheel();
|
||||
frontRightWheel.UpdateWheel();
|
||||
rearLeftWheel.UpdateWheel();
|
||||
rearRightWheel.UpdateWheel();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user