Compare commits
2 commits
8e4303adb0
...
c7242dbe58
Author | SHA1 | Date | |
---|---|---|---|
Mark Joshwel | c7242dbe58 | ||
Mark Joshwel | 72596f0b07 |
File diff suppressed because it is too large
Load diff
|
@ -1,238 +0,0 @@
|
|||
/*
|
||||
* author: ryan lin
|
||||
* date: 30/7/2024
|
||||
* description: making the car go to a game object's position
|
||||
*/
|
||||
|
||||
using System.Collections;
|
||||
using UnityEngine;
|
||||
|
||||
/// <summary>
|
||||
/// making the car go to a game object's position
|
||||
/// </summary>
|
||||
public class AICar : MonoBehaviour
|
||||
{
|
||||
/// <summary>
|
||||
/// reference to the car model
|
||||
/// </summary>
|
||||
[SerializeField] private Transform carPosition;
|
||||
|
||||
/// <summary>
|
||||
/// the radius of which the car can stop in
|
||||
/// </summary>
|
||||
[SerializeField] private float stoppingDistance;
|
||||
|
||||
/// <summary>
|
||||
/// the speed of which the car needs to be at/under to when it is in the slowed state
|
||||
/// </summary>
|
||||
[SerializeField] private float slowingSpeed;
|
||||
|
||||
/// <summary>
|
||||
/// the radius that the car switches to the slowed state
|
||||
/// </summary>
|
||||
[SerializeField] private float slowingDistance;
|
||||
|
||||
/// <summary>
|
||||
/// the distance that the car is allowed to reverse
|
||||
/// </summary>
|
||||
[SerializeField] private float reverseDist;
|
||||
|
||||
/// <summary>
|
||||
/// the acceleration input for the car script
|
||||
/// </summary>
|
||||
private float _accelerationInput;
|
||||
|
||||
/// <summary>
|
||||
/// the angular input for the car script
|
||||
/// </summary>
|
||||
private float _angularDirection;
|
||||
|
||||
/// <summary>
|
||||
/// reference to the car script
|
||||
/// </summary>
|
||||
private CarController _car;
|
||||
|
||||
/// <summary>
|
||||
/// the current state the car is in for fsm
|
||||
/// </summary>
|
||||
private string _currentState;
|
||||
|
||||
/// <summary>
|
||||
/// the distance that the car is from the target
|
||||
/// </summary>
|
||||
private float _distanceToTarget;
|
||||
|
||||
/// <summary>
|
||||
/// the transform of the target
|
||||
/// </summary>
|
||||
private Transform _driveTarget;
|
||||
|
||||
private NextState _nextState;
|
||||
|
||||
/// <summary>
|
||||
/// the turn input for the car script
|
||||
/// </summary>
|
||||
private float _turnInput;
|
||||
|
||||
/// <summary>
|
||||
/// the car is in front or behind the target, a positive will be returned if the car is in front
|
||||
/// </summary>
|
||||
private float _verticalDirection;
|
||||
|
||||
/// <summary>
|
||||
/// initialise values
|
||||
/// </summary>
|
||||
private void Awake()
|
||||
{
|
||||
var hasChild = false;
|
||||
var i = 0;
|
||||
while (!hasChild)
|
||||
{
|
||||
var test = gameObject.transform.GetChild(i).gameObject;
|
||||
if (test.name == "Target")
|
||||
{
|
||||
_driveTarget = test.transform;
|
||||
hasChild = true;
|
||||
}
|
||||
else
|
||||
{
|
||||
i++;
|
||||
}
|
||||
}
|
||||
|
||||
_car = GetComponent<CarController>();
|
||||
_nextState = NextState.Stopped;
|
||||
_currentState = "Stopped";
|
||||
StartCoroutine(_currentState);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// to update the next state value when it changes
|
||||
/// </summary>
|
||||
private void Update()
|
||||
{
|
||||
if (_nextState == NextState.Stopped)
|
||||
_currentState = "Stopped";
|
||||
else if (_nextState == NextState.Slowed)
|
||||
_currentState = "Slowed";
|
||||
else
|
||||
_currentState = "Driving";
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// is called at the end of states to call the next state
|
||||
/// </summary>
|
||||
private void ChangeState()
|
||||
{
|
||||
StartCoroutine(_currentState);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// a function that allows the car to steer
|
||||
/// </summary>
|
||||
private void Steering()
|
||||
{
|
||||
_angularDirection = Vector3.SignedAngle(carPosition.transform.forward,
|
||||
(_driveTarget.position - carPosition.transform.position).normalized, Vector3.up);
|
||||
if (_angularDirection < 0)
|
||||
_turnInput = -1;
|
||||
else
|
||||
_turnInput = 1;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// to put the car in a stopped state
|
||||
/// </summary>
|
||||
private IEnumerator Stopped()
|
||||
{
|
||||
while (_nextState != NextState.Stopped)
|
||||
{
|
||||
_distanceToTarget = Vector3.Distance(carPosition.position, _driveTarget.position);
|
||||
_car.braking = true;
|
||||
_accelerationInput = 0f;
|
||||
_turnInput = 0f;
|
||||
if (_distanceToTarget > stoppingDistance) _nextState = NextState.Slowed;
|
||||
yield return new WaitForSeconds(1);
|
||||
}
|
||||
|
||||
ChangeState();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// to put the car in a slowed state
|
||||
/// </summary>
|
||||
private IEnumerator Slowed()
|
||||
{
|
||||
_car.braking = false;
|
||||
while (_nextState != NextState.Slowed)
|
||||
{
|
||||
_verticalDirection = Vector3.Dot(carPosition.transform.forward,
|
||||
(_driveTarget.position - carPosition.transform.position).normalized);
|
||||
_distanceToTarget = Vector3.Distance(carPosition.position, _driveTarget.position);
|
||||
if (_verticalDirection > 0)
|
||||
{
|
||||
if (carPosition.GetComponent<Rigidbody>().velocity.magnitude > slowingSpeed)
|
||||
_accelerationInput = -1;
|
||||
else
|
||||
_accelerationInput = 1;
|
||||
}
|
||||
else
|
||||
{
|
||||
if (_distanceToTarget < reverseDist)
|
||||
_accelerationInput = -1;
|
||||
else
|
||||
_accelerationInput = 1;
|
||||
}
|
||||
|
||||
Steering();
|
||||
SlowedCheck();
|
||||
_car.SetInputs(_accelerationInput, _turnInput);
|
||||
yield return new WaitForEndOfFrame();
|
||||
}
|
||||
|
||||
ChangeState();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// to put the car in a driving state
|
||||
/// </summary>
|
||||
private IEnumerator Driving()
|
||||
{
|
||||
_car.braking = false;
|
||||
while (_nextState != NextState.Driving)
|
||||
{
|
||||
_verticalDirection = Vector3.Dot(carPosition.transform.forward,
|
||||
(_driveTarget.position - carPosition.transform.position).normalized);
|
||||
_distanceToTarget = Vector3.Distance(carPosition.position, _driveTarget.position);
|
||||
_accelerationInput = 1;
|
||||
|
||||
|
||||
Steering();
|
||||
if (_distanceToTarget < slowingDistance) _nextState = NextState.Slowed;
|
||||
_car.SetInputs(_accelerationInput, _turnInput);
|
||||
yield return new WaitForEndOfFrame();
|
||||
}
|
||||
|
||||
ChangeState();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// a function to check if the car should be a slowed state
|
||||
/// </summary>
|
||||
private void SlowedCheck()
|
||||
{
|
||||
if (_distanceToTarget < stoppingDistance) _nextState = NextState.Stopped;
|
||||
|
||||
if (_distanceToTarget > slowingDistance) _nextState = NextState.Driving;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// enum of states that the car can be in
|
||||
/// </summary>
|
||||
private enum NextState
|
||||
{
|
||||
Slowed,
|
||||
Stopped,
|
||||
Driving
|
||||
}
|
||||
}
|
|
@ -1,95 +0,0 @@
|
|||
/*
|
||||
* author: ryan lin
|
||||
* date: 08/08/2024
|
||||
* description: to cull and spawn AI people
|
||||
*/
|
||||
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
using Random = System.Random;
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// to cull and spawn AI people
|
||||
/// </summary>
|
||||
public class AIManager : MonoBehaviour
|
||||
{
|
||||
/// <summary>
|
||||
/// a reference to the player
|
||||
/// </summary>
|
||||
public Transform player;
|
||||
|
||||
/// <summary>
|
||||
/// the distance of which the AI would be "killed"
|
||||
/// </summary>
|
||||
public float cullingDistance;
|
||||
|
||||
/// <summary>
|
||||
/// the prefab to spawn
|
||||
/// </summary>
|
||||
public GameObject aiPrefab;
|
||||
|
||||
/// <summary>
|
||||
/// the maximum number of AI that can be spawned at any time
|
||||
/// </summary>
|
||||
public int maxAI;
|
||||
|
||||
/// <summary>
|
||||
/// An array that contains the game objects of the AI objects
|
||||
/// </summary>
|
||||
private GameObject[] _ais;
|
||||
|
||||
/// <summary>
|
||||
/// a temporary float to find the distance between the player and the AI
|
||||
/// </summary>
|
||||
private float _distance;
|
||||
/// <summary>
|
||||
/// AI Spawn locations
|
||||
/// </summary>
|
||||
[SerializeField]private List<Transform> aiSpawn;
|
||||
|
||||
/// <summary>
|
||||
/// to start the manager loop
|
||||
/// </summary>
|
||||
private void Start()
|
||||
{
|
||||
StartCoroutine(Manager());
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// to show the range in the inspector when the object is selected
|
||||
/// </summary>
|
||||
private void OnDrawGizmosSelected()
|
||||
{
|
||||
Gizmos.color = Color.yellow;
|
||||
Gizmos.DrawWireSphere(player.position, cullingDistance);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// to allow the IEnumerator to destroy or instantiate AI
|
||||
/// </summary>
|
||||
private IEnumerator Manager()
|
||||
{
|
||||
while (true)
|
||||
{
|
||||
// FIXME: feels weird
|
||||
_ais = GameObject.FindGameObjectsWithTag("AIs");
|
||||
if (_ais.Length < maxAI)
|
||||
{
|
||||
var rand = new Random();
|
||||
var spawnNo=rand.Next(0, aiSpawn.Count);
|
||||
|
||||
var instance = Instantiate(aiPrefab, aiSpawn[spawnNo]);
|
||||
}
|
||||
|
||||
foreach (var i in _ais)
|
||||
{
|
||||
_distance = Vector3.Distance(i.transform.position, player.position);
|
||||
if (_distance > cullingDistance) Destroy(i.gameObject);
|
||||
}
|
||||
|
||||
yield return new WaitForSeconds(1);
|
||||
}
|
||||
}
|
||||
}
|
|
@ -47,11 +47,6 @@ public class AICar : MonoBehaviour
|
|||
/// </summary>
|
||||
private float _angularDirection;
|
||||
|
||||
/// <summary>
|
||||
/// reference to the car script
|
||||
/// </summary>
|
||||
private CarController _car;
|
||||
|
||||
/// <summary>
|
||||
/// the current state the car is in for fsm
|
||||
/// </summary>
|
||||
|
@ -74,6 +69,11 @@ public class AICar : MonoBehaviour
|
|||
/// </summary>
|
||||
private float _turnInput;
|
||||
|
||||
/// <summary>
|
||||
/// reference to the car script
|
||||
/// </summary>
|
||||
private VehicleController _vehicle;
|
||||
|
||||
/// <summary>
|
||||
/// the car is in front or behind the target, a positive will be returned if the car is in front
|
||||
/// </summary>
|
||||
|
@ -100,7 +100,7 @@ private void Awake()
|
|||
}
|
||||
}
|
||||
|
||||
_car = GetComponent<CarController>();
|
||||
_vehicle = GetComponent<VehicleController>();
|
||||
_nextState = NextState.Stopped;
|
||||
_currentState = "Stopped";
|
||||
StartCoroutine(_currentState);
|
||||
|
@ -148,7 +148,7 @@ private IEnumerator Stopped()
|
|||
while (_nextState != NextState.Stopped)
|
||||
{
|
||||
_distanceToTarget = Vector3.Distance(carPosition.position, _driveTarget.position);
|
||||
_car.braking = true;
|
||||
_vehicle.braking = true;
|
||||
_accelerationInput = 0f;
|
||||
_turnInput = 0f;
|
||||
if (_distanceToTarget > stoppingDistance) _nextState = NextState.Slowed;
|
||||
|
@ -163,7 +163,7 @@ private IEnumerator Stopped()
|
|||
/// </summary>
|
||||
private IEnumerator Slowed()
|
||||
{
|
||||
_car.braking = false;
|
||||
_vehicle.braking = false;
|
||||
while (_nextState != NextState.Slowed)
|
||||
{
|
||||
_verticalDirection = Vector3.Dot(carPosition.transform.forward,
|
||||
|
@ -186,7 +186,7 @@ private IEnumerator Slowed()
|
|||
|
||||
Steering();
|
||||
SlowedCheck();
|
||||
_car.SetInputs(_accelerationInput, _turnInput);
|
||||
_vehicle.SetInputs(_accelerationInput, _turnInput);
|
||||
yield return new WaitForEndOfFrame();
|
||||
}
|
||||
|
||||
|
@ -198,7 +198,7 @@ private IEnumerator Slowed()
|
|||
/// </summary>
|
||||
private IEnumerator Driving()
|
||||
{
|
||||
_car.braking = false;
|
||||
_vehicle.braking = false;
|
||||
while (_nextState != NextState.Driving)
|
||||
{
|
||||
_verticalDirection = Vector3.Dot(carPosition.transform.forward,
|
||||
|
@ -209,7 +209,7 @@ private IEnumerator Driving()
|
|||
|
||||
Steering();
|
||||
if (_distanceToTarget < slowingDistance) _nextState = NextState.Slowed;
|
||||
_car.SetInputs(_accelerationInput, _turnInput);
|
||||
_vehicle.SetInputs(_accelerationInput, _turnInput);
|
||||
yield return new WaitForEndOfFrame();
|
||||
}
|
||||
|
||||
|
|
|
@ -35,6 +35,11 @@ public class AIManager : MonoBehaviour
|
|||
/// </summary>
|
||||
public int maxAI;
|
||||
|
||||
/// <summary>
|
||||
/// AI Spawn locations
|
||||
/// </summary>
|
||||
[SerializeField] private List<Transform> aiSpawn;
|
||||
|
||||
/// <summary>
|
||||
/// An array that contains the game objects of the AI objects
|
||||
/// </summary>
|
||||
|
@ -44,10 +49,6 @@ public class AIManager : MonoBehaviour
|
|||
/// a temporary float to find the distance between the player and the AI
|
||||
/// </summary>
|
||||
private float _distance;
|
||||
/// <summary>
|
||||
/// AI Spawn locations
|
||||
/// </summary>
|
||||
[SerializeField]private List<Transform> aiSpawn;
|
||||
|
||||
/// <summary>
|
||||
/// to start the manager loop
|
||||
|
|
28
RunningLateGame/Assets/Scripts/CommonVehicle.cs
Normal file
28
RunningLateGame/Assets/Scripts/CommonVehicle.cs
Normal file
|
@ -0,0 +1,28 @@
|
|||
/*
|
||||
* author: ryan lin, mark joshwel
|
||||
* date: 15/08/2024
|
||||
* description: common vehicular behaviour
|
||||
*/
|
||||
|
||||
using UnityEngine;
|
||||
|
||||
/// <summary>
|
||||
/// logic to move a vehicle to a target position
|
||||
/// </summary>
|
||||
public class CommonVehicle : MonoBehaviour
|
||||
{
|
||||
/// <summary>
|
||||
/// game object that the vehicle is moving towards
|
||||
/// </summary>
|
||||
[SerializeField] private GameObject targetWaypoint;
|
||||
|
||||
/// <summary>
|
||||
/// enum of states that the vehicle can be in
|
||||
/// </summary>
|
||||
private enum State
|
||||
{
|
||||
Stopped,
|
||||
Slowed,
|
||||
Driving
|
||||
}
|
||||
}
|
|
@ -1,5 +1,5 @@
|
|||
fileFormatVersion: 2
|
||||
guid: bc82a40176164f02897928bd573bc2ca
|
||||
guid: e1a5b97ac163ed54bb06c559615dfdd5
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
|
@ -1,5 +1,5 @@
|
|||
/*
|
||||
* author: ryan lin
|
||||
* author: ryan lin, mark joshwel
|
||||
* date: 06/08/2024
|
||||
* description: a controller for the car
|
||||
*/
|
||||
|
@ -12,7 +12,7 @@
|
|||
/// <summary>
|
||||
/// a controller for the car
|
||||
/// </summary>
|
||||
public class CarController : MonoBehaviour
|
||||
public class VehicleController : MonoBehaviour
|
||||
{
|
||||
/// <summary>
|
||||
/// enum for if the wheel is the front or back wheel
|
||||
|
@ -51,12 +51,12 @@ public enum Axel
|
|||
/// <summary>
|
||||
/// inputs for acceleration
|
||||
/// </summary>
|
||||
[SerializeField] private float _currentAcceleration;
|
||||
[SerializeField] private float currentAcceleration;
|
||||
|
||||
/// <summary>
|
||||
/// inputs for turning
|
||||
/// </summary>
|
||||
[SerializeField] private float _currentTurn;
|
||||
[SerializeField] private float currentTurn;
|
||||
|
||||
/// <summary>
|
||||
/// to move the car
|
||||
|
@ -73,23 +73,23 @@ private void FixedUpdate()
|
|||
/// </summary>
|
||||
public void SetInputs(float forwardAmount, float turnAmount)
|
||||
{
|
||||
_currentAcceleration = forwardAmount * motorTorque;
|
||||
_currentTurn = turnAmount * turnAngle;
|
||||
currentAcceleration = forwardAmount * motorTorque;
|
||||
currentTurn = turnAmount * turnAngle;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// to move the car forwards or backwards
|
||||
/// </summary>
|
||||
public void Move()
|
||||
private void Move()
|
||||
{
|
||||
foreach (var wheel in wheels)
|
||||
wheel.wheelCollider.motorTorque = _currentAcceleration;
|
||||
wheel.wheelCollider.motorTorque = currentAcceleration;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// to stop the car
|
||||
/// </summary>
|
||||
public void Brake()
|
||||
private void Brake()
|
||||
{
|
||||
if (braking)
|
||||
foreach (var wheel in wheels)
|
||||
|
@ -102,14 +102,10 @@ public void Brake()
|
|||
/// <summary>
|
||||
/// to turn the car
|
||||
/// </summary>
|
||||
public void Steering()
|
||||
private void Steering()
|
||||
{
|
||||
foreach (var wheel in wheels)
|
||||
if (wheel.axel == Axel.Front)
|
||||
{
|
||||
wheel.wheelCollider.steerAngle = _currentTurn;
|
||||
|
||||
}
|
||||
foreach (var wheel in wheels.Where(wheel => wheel.axel == Axel.Front))
|
||||
wheel.wheelCollider.steerAngle = currentTurn;
|
||||
}
|
||||
|
||||
/// <summary>
|
15
RunningLateGame/Assets/Scripts/VehicleWaypoint.cs
Normal file
15
RunningLateGame/Assets/Scripts/VehicleWaypoint.cs
Normal file
|
@ -0,0 +1,15 @@
|
|||
/*
|
||||
* author: mark joshwel
|
||||
* date: 15/08/2024
|
||||
* description: vehicular target
|
||||
*/
|
||||
|
||||
using UnityEngine;
|
||||
|
||||
public class VehicleWaypoint : MonoBehaviour
|
||||
{
|
||||
/// <summary>
|
||||
/// next vehicle target waypoint for the vehicle to move to
|
||||
/// </summary>
|
||||
[SerializeField] private GameObject nextTarget;
|
||||
}
|
|
@ -1,5 +1,5 @@
|
|||
fileFormatVersion: 2
|
||||
guid: 6ed3090768dbec24b922f887029b3132
|
||||
guid: 0ad7b657ea6a4b94781a78d6f2b6f475
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
Reference in a new issue