Hi,
im working on a basic enemyAI and ive got stuck.
The AI is simple, i use empty gameobjects as waypoints wich my enemy will search using :
waypoints = GameObject.FindGameObjectsWithTag(“EnemyWaypoint”);
the AI got three states, Patrol, Chase and Attack, all three states are working fine but the only problem ive got is when i put another enemy on the scene.
Then both enemys will navigate the same path. Ive tried to make the waypoints array in the Patrol script public so i can drop waypoints to it but i cant get it done.
am i missing something?
And how can i get multiple enemys patrol the same area with different waypoints?
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class NPCBaseFinalStateMachine : StateMachineBehaviour {
public GameObject NPC;
public UnityEngine.AI.NavMeshAgent agent;
public GameObject opponent;
public float speed = 2.0f;
public float rotationSpeed = 1.0f;
public float accuracy = 3.0f;
public override void OnStateEnter(Animator animator, AnimatorStateInfo stateInfo, int layerIndex)
{
NPC = animator.gameObject;
opponent = NPC.GetComponent<EnemyAI>().GetPlayer();
agent = NPC.GetComponent<UnityEngine.AI.NavMeshAgent>();
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Patrol : NPCBaseFinalStateMachine
{
public GameObject[] waypoints;
int currentWP;
private void Awake()
{
waypoints = GameObject.FindGameObjectsWithTag("EnemyWaypoint");
}
// OnStateEnter is called when a transition starts and the state machine starts to evaluate this state
override public void OnStateEnter(Animator animator, AnimatorStateInfo stateInfo, int layerIndex)
{
base.OnStateEnter(animator, stateInfo, layerIndex);
agent.enabled = true;
currentWP = 0;
}
// OnStateUpdate is called on each Update frame between OnStateEnter and OnStateExit callbacks
override public void OnStateUpdate(Animator animator, AnimatorStateInfo stateInfo, int layerIndex)
{
if (waypoints.Length == 0) return;
if (Vector3.Distance(waypoints[currentWP].transform.position, NPC.transform.position) < accuracy)
{
currentWP++;
if (currentWP >= waypoints.Length)
{
currentWP = 0;
}
}
agent.SetDestination(waypoints[currentWP].transform.position);
/*
var direction = waypoints[currentWP].transform.position - NPC.transform.position;
NPC.transform.rotation = Quaternion.Slerp(NPC.transform.rotation, Quaternion.LookRotation(direction),
rotationSpeed * Time.deltaTime);
NPC.transform.Translate(0, 0, Time.deltaTime * speed);
*/
}
// OnStateExit is called when a transition ends and the state machine finishes evaluating this state
override public void OnStateExit(Animator animator, AnimatorStateInfo stateInfo, int layerIndex)
{
agent.enabled = false;
}
}
Enemy AI Script if needed
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class EnemyAI : MonoBehaviour {
Animator anim;
public GameObject player;
public GameObject projectile;
public GameObject turret;
public GameObject GetPlayer()
{
return player;
}
void Fire()
{
Instantiate(projectile, turret.transform.position, turret.transform.rotation);
}
public void StopFiring()
{
CancelInvoke("Fire");
}
public void StartFiring()
{
InvokeRepeating("Fire", 1f, 1f);
}
// Use this for initialization
void Start ()
{
anim = GetComponent<Animator>();
}
// Update is called once per frame
void Update ()
{
anim.SetFloat("distance", Vector3.Distance(transform.position, player.transform.position));
}
}