- Home /
Enemies Follow Player2
I need help making my "Spawner" spawn Enemies that follow my Player i have three that work and follow but when my spawners "turn on" they go to where my Player began, below is my code i have for my spawner using System.Collections; using System.Collections.Generic; using UnityEngine;
public class TimedSpawn : MonoBehaviour {
public GameObject spawnee;
public bool stopSpawning;
public float spawnTime;
public float spawnDelay;
void Start()
{
InvokeRepeating("SpawnObject", spawnTime, spawnDelay);
}
public void SpawnObject()
{
Instantiate(spawnee, transform.position, transform.rotation);
if (stopSpawning)
{
CancelInvoke("SpawnObject");
}
}
} Here is what i use to make my Enemies move using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.AI;
public class NPCMove : MonoBehaviour {
[SerializeField]
Transform _destination;
NavMeshAgent _navMeshAgent;
internal Transform destination;
// Use this for initialization
void Start()
{
_navMeshAgent = this.GetComponent<NavMeshAgent>();
if (_navMeshAgent == null)
{
Debug.LogError("The nav mesh agent component is not attached to " + gameObject.name);
}
else
{
SetDestination();
}
}
private void SetDestination()
{
if(_destination != null)
{
Vector3 targetVector = _destination.transform.position;
_navMeshAgent.SetDestination(targetVector);
}
}
private void Update()
{
SetDestination();
}
}
whatever _destination is, it's not the player you want to follow.
Your answer
Follow this Question
Related Questions
Random Running and Follow at Certain Distance 0 Answers
Resize Array Based on Value 2 Answers
Follower Airplane problem 1 Answer
enemy flying through air when targeting player 1 Answer
AI fleeing from multiple enemies 0 Answers