game: adding the car manger Scripts

This commit is contained in:
Sc0rch-thinks 2024-08-02 12:23:06 +08:00
parent 6f6db679fc
commit a5890e976a
8 changed files with 1100 additions and 69 deletions

File diff suppressed because it is too large Load diff

View file

@ -1,5 +1,5 @@
fileFormatVersion: 2
guid: 8237b59589e7d0f43920c1a2f5ae94f8
guid: c5b15e08603268b499e9f7d38414aa1b
NativeFormatImporter:
externalObjects: {}
mainObjectFileID: 23800000

View file

@ -0,0 +1,64 @@
using System;
using System.Collections.Generic;
using UnityEngine;
public class CarController : MonoBehaviour
{
public enum Axel
{
Front,
Rear
}
public float acceleration;
public float turnAngle;
public List<Wheel> wheels;
public float brakeForce = 50.0f;
private float _currentAcceleration;
private float _currentTurn;
public bool braking;
private void FixedUpdate()
{
Move();
Steering();
Brake();
}
public void SetInputs(float forwardAmount,float turnAmount)
{
_currentAcceleration = forwardAmount*acceleration;
_currentTurn = turnAmount*turnAngle;
}
public void Move()
{
foreach (var wheel in wheels) wheel.wheelCollider.motorTorque = _currentAcceleration;
}
public void Brake()
{
if (braking)
foreach (var wheel in wheels)
wheel.wheelCollider.brakeTorque = brakeForce;
else
foreach (var wheel in wheels)
wheel.wheelCollider.brakeTorque = 0f;
}
public void Steering()
{
foreach (var wheel in wheels)
if (wheel.axel == Axel.Front)
wheel.wheelCollider.steerAngle = _currentTurn;
}
[Serializable]
public struct Wheel
{
public GameObject wheelModel;
public WheelCollider wheelCollider;
public Axel axel;
}
}

View file

@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 6da9430305b32f446a2517f4a73fdf57
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View file

@ -0,0 +1,19 @@
using System;
using UnityEngine;
public class AICar : MonoBehaviour
{
private CarController _carController;
private void Awake()
{
_carController = GetComponent<CarController>();
}
private void Update()
{
float forwardAmount = 1f;
float turnAmount = 1f;
_carController.SetInputs(forwardAmount,turnAmount);
}
}

View file

@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: bc82a40176164f02897928bd573bc2ca
timeCreated: 1722499203