Enemy not following player, navmeshagent AI
Hello, I am having a bit of a problem. It’s not huge I guess as I could just put the player into the scene and everything works fine. However, I would like the player to be able to spawn in.
The problem I am having is if I instantiate the player the enemies won’t follow the player, even after dropping the prefab into the game object. If I drop the player into the scene and then put the player into the game object it works fine though.
Any help is greatly appreciated.
This is my code to grab the gameobject to folow.
using UnityEngine;
public class PlayerManager : MonoBehaviour
{
#region Singleton
public static PlayerManager instance;
private void Awake()
{
instance = this;
}
#endregion
public GameObject player;
}
this is the code of the enemyai
using UnityEngine;
using UnityEngine.AI;
public class EnemyAI : MonoBehaviour
{
public float lookRadius = 5f;
Transform target;
NavMeshAgent agent;
private void Start()
{
target = PlayerManager.instance.player.transform;
agent = GetComponent<NavMeshAgent>();
}
private void Update()
{
float distance = Vector3.Distance(target.position, transform.position);
if (distance <= lookRadius)
{
agent.SetDestination(target.position);
}
}
and this is the code of the instantiate
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class SpawnPlayer : MonoBehaviour
{
[SerializeField]
private Transform _playerSpawnPos;
// Start is called before the first frame update
void Start()
{
StartCoroutine(Spawn());
}
// Update is called once per frame
void Update()
{
}
IEnumerator Spawn()
{
yield return new WaitForSeconds(1.0f);
PlayerSpawn();
}
void PlayerSpawn()
{
Instantiate(_playerSpawnPos, new Vector3(-20f, 4.3f, 9.4f), Quaternion.identity);
}
}