update
This commit is contained in:
8
Assets/Scripts/DebugInfo.cs
Normal file
8
Assets/Scripts/DebugInfo.cs
Normal file
@@ -0,0 +1,8 @@
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
|
||||
public class DebugInfo : MonoBehaviour
|
||||
{
|
||||
|
||||
}
|
||||
11
Assets/Scripts/DebugInfo.cs.meta
Normal file
11
Assets/Scripts/DebugInfo.cs.meta
Normal file
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 6f97ad1a9bdec0d4eb723002348f6a94
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
10
Assets/Scripts/Quit.cs
Normal file
10
Assets/Scripts/Quit.cs
Normal file
@@ -0,0 +1,10 @@
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
|
||||
public class Quit : MonoBehaviour
|
||||
{
|
||||
private void Update() {
|
||||
if(Input.GetKeyDown(KeyCode.Escape)) Application.Quit();
|
||||
}
|
||||
}
|
||||
11
Assets/Scripts/Quit.cs.meta
Normal file
11
Assets/Scripts/Quit.cs.meta
Normal file
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: bcd90519abd2eaa47ab4e721bdbe12c8
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -1,28 +1,45 @@
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
using TMPro;
|
||||
|
||||
public class WheelController : MonoBehaviour
|
||||
{
|
||||
[SerializeField] WheelCollider frontRightWheelCollider;
|
||||
[SerializeField] WheelCollider frontLeftWheelCollider;
|
||||
[SerializeField] WheelCollider rearRightWheelCollider;
|
||||
[SerializeField] WheelCollider rearLeftWheelCollider;
|
||||
[SerializeField] private WheelCollider frontRightWheelCollider;
|
||||
[SerializeField] private WheelCollider frontLeftWheelCollider;
|
||||
[SerializeField] private WheelCollider rearRightWheelCollider;
|
||||
[SerializeField] private WheelCollider rearLeftWheelCollider;
|
||||
|
||||
//wheel meshes
|
||||
[SerializeField] Transform frontRightWheel;
|
||||
[SerializeField] Transform frontLeftWheel;
|
||||
[SerializeField] Transform rearRightWheel;
|
||||
[SerializeField] Transform rearLeftWheel;
|
||||
[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;
|
||||
public float maxTurnAngle = 15f;
|
||||
[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");
|
||||
@@ -35,19 +52,18 @@ public class WheelController : MonoBehaviour
|
||||
}
|
||||
|
||||
//apply acceleration to front wheels| (this determines which wheel drive is the car (fwd, awd, rwd))
|
||||
rearRightWheelCollider.motorTorque = currentAcceleration;
|
||||
rearLeftWheelCollider.motorTorque = currentAcceleration;
|
||||
ApplyAccelerationToWheel(rearRightWheelCollider, currentAcceleration);
|
||||
ApplyAccelerationToWheel(rearLeftWheelCollider, currentAcceleration);
|
||||
|
||||
//break applies to all four wheels
|
||||
frontRightWheelCollider.brakeTorque = currentBrakeForce;
|
||||
frontRightWheelCollider.brakeTorque = currentBrakeForce;
|
||||
frontRightWheelCollider.brakeTorque = currentBrakeForce;
|
||||
frontRightWheelCollider.brakeTorque = currentBrakeForce;
|
||||
ApplyBrakeToWheel(frontLeftWheelCollider, currentBrakeForce);
|
||||
ApplyBrakeToWheel(frontRightWheelCollider, currentBrakeForce);
|
||||
ApplyBrakeToWheel(rearLeftWheelCollider, currentBrakeForce);
|
||||
ApplyBrakeToWheel(rearRightWheelCollider, currentBrakeForce);
|
||||
|
||||
//steering
|
||||
currentTurnAngle = maxTurnAngle * Input.GetAxis("Horizontal");
|
||||
frontLeftWheelCollider.steerAngle = currentTurnAngle;
|
||||
frontRightWheelCollider.steerAngle = currentTurnAngle;
|
||||
currentTurnAngle = maxTurnAngle * Input.GetAxis("Horizontal"); //getting steering input
|
||||
ApplyStreering(currentTurnAngle);
|
||||
|
||||
//update wheel meshes
|
||||
UpdateWheel(frontLeftWheelCollider, frontLeftWheel);
|
||||
@@ -56,6 +72,19 @@ public class WheelController : MonoBehaviour
|
||||
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;
|
||||
|
||||
Reference in New Issue
Block a user