Compare commits
No commits in common. "0e83b9e5414f44fd24a4e50755f85e896320f914" and "f20a7a4fafa3f3228b101bff83c61603a078b314" have entirely different histories.
0e83b9e541
...
f20a7a4faf
3 changed files with 36 additions and 121 deletions
|
@ -1,7 +1,7 @@
|
||||||
/*
|
/*
|
||||||
* author: lin hengrui ryan
|
* author: lin hengrui ryan
|
||||||
* date: 30/7/2024
|
* date: 30/7/2024
|
||||||
* description: AI code for the different AI in the game
|
* description: ai code for the different ai in the game
|
||||||
*/
|
*/
|
||||||
|
|
||||||
using System.Collections;
|
using System.Collections;
|
||||||
|
@ -12,10 +12,23 @@ using Random = System.Random;
|
||||||
public class AI : MonoBehaviour
|
public class AI : MonoBehaviour
|
||||||
{
|
{
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// to save resources when referring to the speed of this AI if its human
|
/// the types of ai that this game object can be
|
||||||
|
/// </summary>
|
||||||
|
public enum EntityType
|
||||||
|
{
|
||||||
|
Human,
|
||||||
|
Car
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// to save resources when referring to the speed of this ai if its a human
|
||||||
/// </summary>
|
/// </summary>
|
||||||
private static readonly int AnimatorSpeed = Animator.StringToHash("Speed");
|
private static readonly int AnimatorSpeed = Animator.StringToHash("Speed");
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// to show the entity types in the inspector as a dropdown
|
||||||
|
/// </summary>
|
||||||
|
public EntityType entityType;
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// set the layers the AI can be on
|
/// set the layers the AI can be on
|
||||||
|
@ -62,32 +75,30 @@ public class AI : MonoBehaviour
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public void Awake()
|
public void Awake()
|
||||||
{
|
{
|
||||||
_animator = GetComponent<Animator>();
|
|
||||||
_currentState = "Strolling";
|
|
||||||
|
|
||||||
|
|
||||||
_agent = GetComponent<NavMeshAgent>();
|
_agent = GetComponent<NavMeshAgent>();
|
||||||
|
if (entityType == EntityType.Human)
|
||||||
|
{
|
||||||
_animator = GetComponent<Animator>();
|
_animator = GetComponent<Animator>();
|
||||||
_currentState = "Strolling";
|
_currentState = "Strolling";
|
||||||
|
}
|
||||||
|
|
||||||
_nextState = _currentState;
|
_nextState = _currentState;
|
||||||
ChangeState();
|
ChangeState();
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// to update the speed of the AI to the animator each frame and to update any state changes to the AI
|
/// to update the speed of the AI to the animator each frame and to update any state changes to the ai
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public void Update()
|
public void Update()
|
||||||
{
|
{
|
||||||
_animator.SetFloat(AnimatorSpeed, _agent.velocity.magnitude);
|
if (entityType == EntityType.Human) _animator.SetFloat(AnimatorSpeed, _agent.velocity.magnitude);
|
||||||
_currentState = _nextState;
|
_currentState = _nextState;
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// to change the scene when needed
|
/// to change the scene when needed
|
||||||
/// </summary>
|
/// </summary>
|
||||||
private void ChangeState()
|
public void ChangeState()
|
||||||
{
|
{
|
||||||
StartCoroutine(_currentState);
|
StartCoroutine(_currentState);
|
||||||
}
|
}
|
||||||
|
@ -117,6 +128,7 @@ public class AI : MonoBehaviour
|
||||||
|
|
||||||
while (_currentState == "Strolling")
|
while (_currentState == "Strolling")
|
||||||
{
|
{
|
||||||
|
Debug.Log("strolling");
|
||||||
if (!_destinationPointSet)
|
if (!_destinationPointSet)
|
||||||
SearchWalkPoint();
|
SearchWalkPoint();
|
||||||
else
|
else
|
||||||
|
|
|
@ -13,11 +13,11 @@ public class CarController : MonoBehaviour
|
||||||
public float acceleration;
|
public float acceleration;
|
||||||
public float turnAngle;
|
public float turnAngle;
|
||||||
public List<Wheel> wheels;
|
public List<Wheel> wheels;
|
||||||
|
public float brakeForce = 50.0f;
|
||||||
public float brakeForce;
|
|
||||||
public bool braking;
|
|
||||||
private float _currentAcceleration;
|
private float _currentAcceleration;
|
||||||
private float _currentTurn;
|
private float _currentTurn;
|
||||||
|
public bool braking;
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
private void FixedUpdate()
|
private void FixedUpdate()
|
||||||
|
@ -32,8 +32,6 @@ public class CarController : MonoBehaviour
|
||||||
_currentAcceleration = forwardAmount*acceleration;
|
_currentAcceleration = forwardAmount*acceleration;
|
||||||
_currentTurn = turnAmount*turnAngle;
|
_currentTurn = turnAmount*turnAngle;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
public void Move()
|
public void Move()
|
||||||
{
|
{
|
||||||
foreach (var wheel in wheels) wheel.wheelCollider.motorTorque = _currentAcceleration;
|
foreach (var wheel in wheels) wheel.wheelCollider.motorTorque = _currentAcceleration;
|
||||||
|
|
|
@ -3,112 +3,17 @@ using UnityEngine;
|
||||||
|
|
||||||
public class AICar : MonoBehaviour
|
public class AICar : MonoBehaviour
|
||||||
{
|
{
|
||||||
[SerializeField] private Transform carPosition;
|
private CarController _carController;
|
||||||
[SerializeField] private float stoppingDistance;
|
|
||||||
[SerializeField] private float slowingSpeed;
|
|
||||||
[SerializeField] private float slowingDistance;
|
|
||||||
[SerializeField] private float reverseDist;
|
|
||||||
private CarController _car;
|
|
||||||
private string _currentState;
|
|
||||||
private Transform _driveTarget;
|
|
||||||
private string _nextState;
|
|
||||||
|
|
||||||
|
|
||||||
private void Awake()
|
private void Awake()
|
||||||
{
|
{
|
||||||
var hasChild = false;
|
_carController = GetComponent<CarController>();
|
||||||
var i = 0;
|
|
||||||
GameObject test;
|
|
||||||
while (!hasChild)
|
|
||||||
{
|
|
||||||
test = gameObject.transform.GetChild(i).gameObject;
|
|
||||||
if (test.name == "Target")
|
|
||||||
{
|
|
||||||
_driveTarget = test.transform;
|
|
||||||
hasChild = true;
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
i++;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
_car = GetComponent<CarController>();
|
|
||||||
_currentState = "Driving";
|
|
||||||
_nextState = _currentState;
|
|
||||||
ChangeState();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private void Update()
|
private void Update()
|
||||||
{
|
{
|
||||||
_currentState = _nextState;
|
float forwardAmount = 1f;
|
||||||
}
|
float turnAmount = 1f;
|
||||||
|
_carController.SetInputs(forwardAmount,turnAmount);
|
||||||
private void ChangeState()
|
|
||||||
{
|
|
||||||
StartCoroutine(_currentState);
|
|
||||||
}
|
|
||||||
|
|
||||||
private IEnumerator Driving()
|
|
||||||
{
|
|
||||||
_car = GetComponent<CarController>();
|
|
||||||
|
|
||||||
while (_currentState == "Driving")
|
|
||||||
{
|
|
||||||
var accelerationAmount = 0f;
|
|
||||||
var turnAmount = 0f;
|
|
||||||
var targetPosition = _driveTarget.position;
|
|
||||||
var dirToMove = (targetPosition - carPosition.transform.position).normalized;
|
|
||||||
var forwardsAmount = Vector3.Dot(carPosition.transform.forward, dirToMove);
|
|
||||||
var directionToDrive = Vector3.SignedAngle(carPosition.transform.forward, dirToMove, Vector3.up);
|
|
||||||
var distanceToTarget = Vector3.Distance(carPosition.position, targetPosition);
|
|
||||||
if (distanceToTarget >= stoppingDistance)
|
|
||||||
{
|
|
||||||
_car.braking = false;
|
|
||||||
//in front of target position
|
|
||||||
if (forwardsAmount > 0)
|
|
||||||
{
|
|
||||||
//closer than the slowing distance
|
|
||||||
if (distanceToTarget <= slowingDistance)
|
|
||||||
{
|
|
||||||
//the car is very fast
|
|
||||||
if (carPosition.GetComponent<Rigidbody>().velocity.magnitude > slowingSpeed)
|
|
||||||
accelerationAmount = -2;
|
|
||||||
else
|
|
||||||
accelerationAmount = 1;
|
|
||||||
}
|
|
||||||
|
|
||||||
else
|
|
||||||
{
|
|
||||||
accelerationAmount = 1;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
if (distanceToTarget < reverseDist)
|
|
||||||
accelerationAmount = -1;
|
|
||||||
else
|
|
||||||
accelerationAmount = 1;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
if (directionToDrive < 0)
|
|
||||||
turnAmount = 1;
|
|
||||||
else if (directionToDrive > 0)
|
|
||||||
turnAmount = -1;
|
|
||||||
else
|
|
||||||
turnAmount = 0;
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
_car.braking = true;
|
|
||||||
accelerationAmount = 0f;
|
|
||||||
turnAmount = 0f;
|
|
||||||
}
|
|
||||||
|
|
||||||
Debug.Log(carPosition.GetComponent<Rigidbody>().velocity.magnitude);
|
|
||||||
_car.SetInputs(accelerationAmount, turnAmount);
|
|
||||||
yield return new WaitForEndOfFrame();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
Reference in a new issue