game: adding the car manger Scripts
This commit is contained in:
parent
efdcacb822
commit
26bd6d49e7
File diff suppressed because it is too large
Load diff
Binary file not shown.
|
@ -1,5 +1,5 @@
|
|||
fileFormatVersion: 2
|
||||
guid: 8237b59589e7d0f43920c1a2f5ae94f8
|
||||
guid: c5b15e08603268b499e9f7d38414aa1b
|
||||
NativeFormatImporter:
|
||||
externalObjects: {}
|
||||
mainObjectFileID: 23800000
|
Binary file not shown.
64
RunningLateGame/Assets/Scripts/CarController.cs
Normal file
64
RunningLateGame/Assets/Scripts/CarController.cs
Normal 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;
|
||||
}
|
||||
}
|
11
RunningLateGame/Assets/Scripts/CarController.cs.meta
Normal file
11
RunningLateGame/Assets/Scripts/CarController.cs.meta
Normal file
|
@ -0,0 +1,11 @@
|
|||
fileFormatVersion: 2
|
||||
guid: 6da9430305b32f446a2517f4a73fdf57
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
19
RunningLateGame/Assets/Scripts/ai car.cs
Normal file
19
RunningLateGame/Assets/Scripts/ai car.cs
Normal 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);
|
||||
}
|
||||
}
|
3
RunningLateGame/Assets/Scripts/ai car.cs.meta
Normal file
3
RunningLateGame/Assets/Scripts/ai car.cs.meta
Normal file
|
@ -0,0 +1,3 @@
|
|||
fileFormatVersion: 2
|
||||
guid: bc82a40176164f02897928bd573bc2ca
|
||||
timeCreated: 1722499203
|
Reference in a new issue