Hi, I’m working my way through the ‘Unity RPG Core Combat Creator’ and I’m currently on ‘Swappable Control Systems’. We are building an AIController for the enemies and it looks like Ben is essentially rewriting an interface for the Fighter class. Is this approach better than basing the AIController directly on the PlayerController? This is my code for the AIController below:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using RPG.Combat;
using System;
namespace RPG.Control
{
public class AIController : MonoBehaviour
{
public float chaseDistance = 5f;
GameObject player;
void Awake(){
player = GameObject.FindWithTag("Player");
}
void Update()
{
if (PlayerInRange()){
if (InteractWithCombat()) return;
}
if (InteractWithMovement()) return;
print("Nothing to do.");
}
void OnDrawGizmos()
{
Gizmos.color = Color.blue;
Gizmos.DrawWireSphere(transform.position, chaseDistance);
}
private float DistanceToPlayer()
{
return Vector3.Distance(player.transform.position, transform.position);
}
private bool PlayerInRange(){
return DistanceToPlayer() <= chaseDistance;
}
private bool InteractWithMovement()
{
GetComponent<Fighter>().Cancel();
return false;
}
private bool InteractWithCombat()
{
if (GetComponent<Fighter>().CanAttack(player.gameObject)) {
GetComponent<Fighter>().Attack(player.gameObject);
return true;
}
return false;
}
}
}```