Hi all,
Sorry ahead of time for the formatting, I can't for the life of me get the spacing to work. Anyways, I'm self taught and looking to understand the State pattern by implementing it into the enemy AI in my project. I've been following the guides on [Game Design Patterns][1] and [Ray Wenderlich][2].
Here are the classes that I have so far:
public class StateMachine : MonoBehaviour
{
[SerializeField] State currentState;
public StateMachine(State startingState)
{
currentState = startingState;
}
// Update is called once per frame
void Update()
{
currentState.Tick();
}
public void ChangeState(State newState)
{
currentState = newState;
}
}
public abstract class State : MonoBehaviour
{
public abstract void Tick();
}
public class PatrolState : State
{
float patrollingSpeed = 1.5f;
float minWorldSpaceX = -8;
float minWorldSpaceY = -4;
float maxWorldSpaceX = 8;
float maxWorldSpaceY = 4;
bool destinationReached;
Vector3 patrolDestination;
public override void Tick()
{
//Patrolling
if (destinationReached == true)
{
//Choose new destination
float xDestination = Random.Range(minWorldSpaceX, maxWorldSpaceX + 1);
float yDestination = Random.Range(minWorldSpaceY, maxWorldSpaceY + 1);
patrolDestination = new Vector3(xDestination, yDestination, 0);
transform.LookAt(patrolDestination);
Debug.Log("Enemy patrolling to " + patrolDestination);
destinationReached = false;
}
else if (destinationReached == false)
{
transform.position = Vector3.MoveTowards(transform.position, patrolDestination, Time.deltaTime * patrollingSpeed);
if (transform.position == patrolDestination)
{
Debug.Log("Destination reached.");
destinationReached = true;
}
}
}
}
public class ChaseState : State
{
GameObject player;
float speed = 3;
public ChaseState(GameObject chaseObject)
{
player = chaseObject;
}
private void Start()
{
}
public override void Tick()
{
transform.LookAt(player.transform);
transform.Translate(transform.forward * speed * Time.deltaTime, Space.World);
}
}
public class EnemyAI : MonoBehaviour, IDestructable
{
[SerializeField] float speed;
[SerializeField] float patrollingSpeed;
public GameObject player;
public int Health { get; set; }
public int MaxHealth { get; set; }
StateMachine stateMachine;
PatrolState patrolState;
ChaseState chaseState;
// Start is called before the first frame update
void Start()
{
Health = 10;
MaxHealth = 5;
patrolState = new PatrolState();
chaseState = new ChaseState(player);
stateMachine = new StateMachine(patrolState);
}
// Update is called once per frame
void Update()
{
Collider2D detection = Physics2D.OverlapCircle(transform.position, 3);
if (detection != null)
{
if (detection.GetComponent() != null)
{
Debug.Log("Player detected");
stateMachine.ChangeState(chaseState);
}
}
}
public void TakeDamage(int damage)
{
Health -= damage;
Debug.Log("I've been hit!");
if (Health <= 0)
{
Kill();
}
}
public void Kill()
{
Destroy(gameObject);
}
private void OnDrawGizmos()
{
Gizmos.DrawWireSphere(transform.position, 3.0f);
}
}
I've attached the State Machine class to my Enemy prefab, but I'm getting an error saying NullReferenceException: Object reference not set to an instance of an object
StateMachine.Update () (at Assets/Scripts/States/StateMachine.cs:17), which leads me to believe that nothing is being passed in when the State Machine is being created.
Like I said, I'm self taught so it's probably something simple, I just can't seem to figure out what's wrong.
[1]: https://gameprogrammingpatterns.com/state.html
[2]: https://www.raywenderlich.com/6034380-state-pattern-using-unity
↧