AI #1
3 changed files with 14 additions and 119 deletions
|
@ -2,7 +2,7 @@
|
||||||
* author: lin hengrui ryan
|
* author: lin hengrui ryan
|
||||||
* date: 30/7/2024
|
* date: 30/7/2024
|
||||||
<<<<<<< Updated upstream
|
<<<<<<< Updated upstream
|
||||||
* description: ai code for the different ai in the game
|
* description: AI code for the different AI in the game
|
||||||
=======
|
=======
|
||||||
* description: AI code for the different AI in the game
|
* description: AI code for the different AI in the game
|
||||||
>>>>>>> Stashed changes
|
>>>>>>> Stashed changes
|
||||||
|
@ -15,36 +15,11 @@ using Random = System.Random;
|
||||||
|
|
||||||
public class AI : MonoBehaviour
|
public class AI : MonoBehaviour
|
||||||
{
|
{
|
||||||
<<<<<<< Updated upstream
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// the types of ai that this game object can be
|
/// to save resources when referring to the speed of this AI if its human
|
||||||
/// </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>
|
|
||||||
/// to save resources when referring to the speed of this AI
|
|
||||||
/// </summary>
|
|
||||||
private static readonly int AnimatorSpeed = Animator.StringToHash("Speed");
|
|
||||||
|
|
||||||
|
|
||||||
>>>>>>> Stashed changes
|
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// set the layers the AI can be on
|
/// set the layers the AI can be on
|
||||||
|
@ -86,63 +61,38 @@ public class AI : MonoBehaviour
|
||||||
/// </summary>
|
/// </summary>
|
||||||
private string _nextState;
|
private string _nextState;
|
||||||
|
|
||||||
<<<<<<< Updated upstream
|
|
||||||
=======
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
>>>>>>> Stashed changes
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// to set initial values
|
/// to set initial values
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public void Awake()
|
public void Awake()
|
||||||
{
|
{
|
||||||
<<<<<<< Updated upstream
|
_animator = GetComponent<Animator>();
|
||||||
_agent = GetComponent<NavMeshAgent>();
|
_currentState = "Strolling";
|
||||||
if (entityType == EntityType.Human)
|
|
||||||
{
|
|
||||||
_animator = GetComponent<Animator>();
|
_agent = GetComponent<NavMeshAgent>();
|
||||||
_currentState = "Strolling";
|
_animator = GetComponent<Animator>();
|
||||||
}
|
_currentState = "Strolling";
|
||||||
=======
|
|
||||||
|
|
||||||
_agent = GetComponent<NavMeshAgent>();
|
|
||||||
_animator = GetComponent<Animator>();
|
|
||||||
_currentState = "Strolling";
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
>>>>>>> Stashed changes
|
|
||||||
|
|
||||||
_nextState = _currentState;
|
_nextState = _currentState;
|
||||||
ChangeState();
|
ChangeState();
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
<<<<<<< Updated upstream
|
|
||||||
/// to update the speed of the AI to the animator each frame and to update any state changes to the ai
|
|
||||||
/// </summary>
|
|
||||||
public void Update()
|
|
||||||
{
|
|
||||||
if (entityType == EntityType.Human) _animator.SetFloat(AnimatorSpeed, _agent.velocity.magnitude);
|
|
||||||
=======
|
|
||||||
/// 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);
|
_animator.SetFloat(AnimatorSpeed, _agent.velocity.magnitude);
|
||||||
>>>>>>> Stashed changes
|
|
||||||
_currentState = _nextState;
|
_currentState = _nextState;
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// to change the scene when needed
|
/// to change the scene when needed
|
||||||
/// </summary>
|
/// </summary>
|
||||||
<<<<<<< Updated upstream
|
|
||||||
public void ChangeState()
|
|
||||||
=======
|
|
||||||
private void ChangeState()
|
private void ChangeState()
|
||||||
>>>>>>> Stashed changes
|
|
||||||
{
|
{
|
||||||
StartCoroutine(_currentState);
|
StartCoroutine(_currentState);
|
||||||
}
|
}
|
||||||
|
@ -172,10 +122,6 @@ public class AI : MonoBehaviour
|
||||||
|
|
||||||
while (_currentState == "Strolling")
|
while (_currentState == "Strolling")
|
||||||
{
|
{
|
||||||
<<<<<<< Updated upstream
|
|
||||||
Debug.Log("strolling");
|
|
||||||
=======
|
|
||||||
>>>>>>> Stashed changes
|
|
||||||
if (!_destinationPointSet)
|
if (!_destinationPointSet)
|
||||||
SearchWalkPoint();
|
SearchWalkPoint();
|
||||||
else
|
else
|
||||||
|
@ -188,9 +134,4 @@ public class AI : MonoBehaviour
|
||||||
|
|
||||||
ChangeState();
|
ChangeState();
|
||||||
}
|
}
|
||||||
<<<<<<< Updated upstream
|
|
||||||
=======
|
|
||||||
|
|
||||||
|
|
||||||
>>>>>>> Stashed changes
|
|
||||||
}
|
}
|
|
@ -13,18 +13,13 @@ public class CarController : MonoBehaviour
|
||||||
public float acceleration;
|
public float acceleration;
|
||||||
public float turnAngle;
|
public float turnAngle;
|
||||||
public List<Wheel> wheels;
|
public List<Wheel> wheels;
|
||||||
<<<<<<< Updated upstream
|
|
||||||
public float brakeForce = 50.0f;
|
|
||||||
=======
|
|
||||||
public float brakeForce;
|
public float brakeForce;
|
||||||
>>>>>>> Stashed changes
|
public bool braking;
|
||||||
private float _currentAcceleration;
|
private float _currentAcceleration;
|
||||||
private float _currentTurn;
|
private float _currentTurn;
|
||||||
public bool braking;
|
|
||||||
|
|
||||||
|
|
||||||
<<<<<<< Updated upstream
|
|
||||||
|
|
||||||
private void FixedUpdate()
|
private void FixedUpdate()
|
||||||
{
|
{
|
||||||
Move();
|
Move();
|
||||||
|
@ -32,26 +27,13 @@ public class CarController : MonoBehaviour
|
||||||
Brake();
|
Brake();
|
||||||
}
|
}
|
||||||
|
|
||||||
public void SetInputs(float forwardAmount,float turnAmount)
|
|
||||||
{
|
|
||||||
_currentAcceleration = forwardAmount*acceleration;
|
|
||||||
_currentTurn = turnAmount*turnAngle;
|
|
||||||
}
|
|
||||||
=======
|
|
||||||
private void FixedUpdate()
|
|
||||||
{
|
|
||||||
Brake();
|
|
||||||
Move();
|
|
||||||
Steering();
|
|
||||||
}
|
|
||||||
|
|
||||||
public void SetInputs(float forwardAmount, float turnAmount)
|
public void SetInputs(float forwardAmount, float turnAmount)
|
||||||
{
|
{
|
||||||
_currentAcceleration = forwardAmount * acceleration;
|
_currentAcceleration = forwardAmount * acceleration;
|
||||||
_currentTurn = turnAmount * turnAngle;
|
_currentTurn = turnAmount * turnAngle;
|
||||||
}
|
}
|
||||||
|
|
||||||
>>>>>>> Stashed changes
|
|
||||||
public void Move()
|
public void Move()
|
||||||
{
|
{
|
||||||
foreach (var wheel in wheels) wheel.wheelCollider.motorTorque = _currentAcceleration;
|
foreach (var wheel in wheels) wheel.wheelCollider.motorTorque = _currentAcceleration;
|
||||||
|
@ -81,9 +63,4 @@ public class CarController : MonoBehaviour
|
||||||
public WheelCollider wheelCollider;
|
public WheelCollider wheelCollider;
|
||||||
public Axel axel;
|
public Axel axel;
|
||||||
}
|
}
|
||||||
<<<<<<< Updated upstream
|
|
||||||
=======
|
|
||||||
|
|
||||||
|
|
||||||
>>>>>>> Stashed changes
|
|
||||||
}
|
}
|
|
@ -1,19 +1,8 @@
|
||||||
<<<<<<< Updated upstream
|
using System.Collections;
|
||||||
using System;
|
|
||||||
=======
|
|
||||||
using System.Collections;
|
|
||||||
>>>>>>> Stashed changes
|
|
||||||
using UnityEngine;
|
using UnityEngine;
|
||||||
|
|
||||||
public class AICar : MonoBehaviour
|
public class AICar : MonoBehaviour
|
||||||
{
|
{
|
||||||
<<<<<<< Updated upstream
|
|
||||||
private CarController _carController;
|
|
||||||
|
|
||||||
private void Awake()
|
|
||||||
{
|
|
||||||
_carController = GetComponent<CarController>();
|
|
||||||
=======
|
|
||||||
[SerializeField] private Transform carPosition;
|
[SerializeField] private Transform carPosition;
|
||||||
[SerializeField] private float stoppingDistance;
|
[SerializeField] private float stoppingDistance;
|
||||||
[SerializeField] private float slowingSpeed;
|
[SerializeField] private float slowingSpeed;
|
||||||
|
@ -23,7 +12,7 @@ public class AICar : MonoBehaviour
|
||||||
private string _currentState;
|
private string _currentState;
|
||||||
private Transform _driveTarget;
|
private Transform _driveTarget;
|
||||||
private string _nextState;
|
private string _nextState;
|
||||||
|
|
||||||
|
|
||||||
private void Awake()
|
private void Awake()
|
||||||
{
|
{
|
||||||
|
@ -48,16 +37,10 @@ public class AICar : MonoBehaviour
|
||||||
_currentState = "Driving";
|
_currentState = "Driving";
|
||||||
_nextState = _currentState;
|
_nextState = _currentState;
|
||||||
ChangeState();
|
ChangeState();
|
||||||
>>>>>>> Stashed changes
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private void Update()
|
private void Update()
|
||||||
{
|
{
|
||||||
<<<<<<< Updated upstream
|
|
||||||
float forwardAmount = 1f;
|
|
||||||
float turnAmount = 1f;
|
|
||||||
_carController.SetInputs(forwardAmount,turnAmount);
|
|
||||||
=======
|
|
||||||
_currentState = _nextState;
|
_currentState = _nextState;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -103,14 +86,9 @@ public class AICar : MonoBehaviour
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
if (distanceToTarget < reverseDist)
|
if (distanceToTarget < reverseDist)
|
||||||
{
|
|
||||||
accelerationAmount = -1;
|
accelerationAmount = -1;
|
||||||
}
|
|
||||||
else
|
else
|
||||||
{
|
|
||||||
accelerationAmount = 1;
|
accelerationAmount = 1;
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@ -132,6 +110,5 @@ public class AICar : MonoBehaviour
|
||||||
_car.SetInputs(accelerationAmount, turnAmount);
|
_car.SetInputs(accelerationAmount, turnAmount);
|
||||||
yield return new WaitForEndOfFrame();
|
yield return new WaitForEndOfFrame();
|
||||||
}
|
}
|
||||||
>>>>>>> Stashed changes
|
|
||||||
}
|
}
|
||||||
}
|
}
|
Reference in a new issue