This repository has been archived on 2024-08-16. You can view files and clone it, but cannot push or open issues or pull requests.
sota/RunningLateGame/Assets/Scripts/AiCar.cs

232 lines
5.4 KiB
C#
Raw Normal View History

/*
* author: ryan lin
* date: 30/7/2024
* description: vehicular ai behaviour
*/
using System.Collections;
2024-08-07 06:55:21 +00:00
using UnityEngine;
/// <summary>
/// TODO
/// </summary>
2024-08-07 06:55:21 +00:00
public class AICar : MonoBehaviour
{
/// <summary>
/// TODO
/// </summary>
2024-08-07 06:55:21 +00:00
[SerializeField] private Transform carPosition;
/// <summary>
/// TODO
/// </summary>
2024-08-07 06:55:21 +00:00
[SerializeField] private float stoppingDistance;
/// <summary>
/// TODO
/// </summary>
2024-08-07 06:55:21 +00:00
[SerializeField] private float slowingSpeed;
/// <summary>
/// TODO
/// </summary>
2024-08-07 06:55:21 +00:00
[SerializeField] private float slowingDistance;
/// <summary>
/// TODO
/// </summary>
2024-08-07 06:55:21 +00:00
[SerializeField] private float reverseDist;
/// <summary>
/// TODO
/// </summary>
2024-08-07 06:55:21 +00:00
private float _accelerationInput;
/// <summary>
/// TODO
/// </summary>
2024-08-07 06:55:21 +00:00
private float _angularDirection;
/// <summary>
/// TODO
/// </summary>
private CarController _car;
/// <summary>
/// TODO
/// </summary>
private string _currentState;
/// <summary>
/// TODO
/// </summary>
2024-08-07 06:55:21 +00:00
private Vector3 _dirToMove;
/// <summary>
/// TODO
/// </summary>
2024-08-07 06:55:21 +00:00
private float _distanceToTarget;
/// <summary>
/// TODO
/// </summary>
2024-08-07 06:55:21 +00:00
private Transform _driveTarget;
/// <summary>
/// TODO
/// </summary>
// FIXME: use an enum or something
private string _nextState;
/// <summary>
/// TODO
/// </summary>
2024-08-07 06:55:21 +00:00
private float _turnInput;
/// <summary>
/// TODO
/// </summary>
2024-08-07 06:55:21 +00:00
private float _verticalDirection;
/// <summary>
/// TODO
/// </summary>
2024-08-07 06:55:21 +00:00
private void Awake()
{
var hasChild = false;
var i = 0;
while (!hasChild)
{
2024-08-09 18:27:32 +00:00
var test = gameObject.transform.GetChild(i).gameObject;
2024-08-07 06:55:21 +00:00
if (test.name == "Target")
{
_driveTarget = test.transform;
hasChild = true;
}
else
{
i++;
}
}
_car = GetComponent<CarController>();
_currentState = "Stopped";
_nextState = _currentState;
ChangeState();
}
/// <summary>
/// TODO
/// </summary>
2024-08-07 06:55:21 +00:00
private void Update()
{
_currentState = _nextState;
}
/// <summary>
/// TODO
/// </summary>
2024-08-07 06:55:21 +00:00
private void ChangeState()
{
StartCoroutine(_currentState);
}
/// <summary>
/// TODO
/// </summary>
2024-08-07 06:55:21 +00:00
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>
/// TODO
/// </summary>
2024-08-07 06:55:21 +00:00
private IEnumerator Stopped()
{
while (_currentState == "Stopped")
{
_distanceToTarget = Vector3.Distance(carPosition.position, _driveTarget.position);
2024-08-07 06:55:21 +00:00
_car.braking = true;
_accelerationInput = 0f;
_turnInput = 0f;
if (_distanceToTarget > stoppingDistance) _nextState = "Slowed";
2024-08-07 06:55:21 +00:00
yield return new WaitForSeconds(1);
}
2024-08-07 06:55:21 +00:00
ChangeState();
}
/// <summary>
/// TODO
/// </summary>
2024-08-07 06:55:21 +00:00
private IEnumerator Slowed()
{
_car.braking = false;
while (_currentState == "Slowed")
{
_verticalDirection = Vector3.Dot(carPosition.transform.forward,
(_driveTarget.position - carPosition.transform.position).normalized);
_distanceToTarget = Vector3.Distance(carPosition.position, _driveTarget.position);
if (_verticalDirection > 0)
2024-08-07 06:55:21 +00:00
{
if (carPosition.GetComponent<Rigidbody>().velocity.magnitude > slowingSpeed)
_accelerationInput = -1;
else
_accelerationInput = 1;
}
else
{
if (_distanceToTarget < reverseDist)
_accelerationInput = -1;
else
_accelerationInput = 1;
}
2024-08-07 06:55:21 +00:00
Steering();
SlowedCheck();
_car.SetInputs(_accelerationInput, _turnInput);
yield return new WaitForEndOfFrame();
}
2024-08-07 06:55:21 +00:00
ChangeState();
}
/// <summary>
/// TODO
/// </summary>
2024-08-07 06:55:21 +00:00
private IEnumerator Driving()
{
_car.braking = false;
while (_currentState == "Driving")
{
_verticalDirection = Vector3.Dot(carPosition.transform.forward,
(_driveTarget.position - carPosition.transform.position).normalized);
_distanceToTarget = Vector3.Distance(carPosition.position, _driveTarget.position);
_accelerationInput = 1;
2024-08-07 06:55:21 +00:00
Steering();
if (_distanceToTarget < slowingDistance) _nextState = "Slowed";
_car.SetInputs(_accelerationInput, _turnInput);
yield return new WaitForEndOfFrame();
}
2024-08-07 06:55:21 +00:00
ChangeState();
}
/// <summary>
/// TODO
/// </summary>
2024-08-07 06:55:21 +00:00
private void SlowedCheck()
{
if (_distanceToTarget < stoppingDistance) _nextState = "Stopped";
2024-08-07 06:55:21 +00:00
if (_distanceToTarget > slowingDistance) _nextState = "Driving";
}
}