I have a 2 player game. I need the navmesh enemy to pick a player to chase(preferably when it enters a trigger or by distance or even random if need be). If one player is destroyed how can I swap the navmesh destination without getting a null destination error. I've tried multiple solutions but can't seem to find quite what i'm looking for. Below is my solution thus far.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.AI; //be sure to include when using NavMesh
public class EnemyNav : MonoBehaviour
{
//public Transform playerTarget;
private NavMeshAgent agent;
// public Transform Player1;
// public Transform Player2;
public Transform[] players;
// Use this for initialization
void Start ()
{
agent = GetComponent();
}
// Update is called once per frame
void Update ()
{
// agent.destination = playerTarget.position;
}
void OnTriggerStay(Collider other)
{
int rnd = Random.Range(0, players.Length);
agent.destination = players[rnd].position;
/*
if(other.tag == "Player1")
{
agent.destination = Player1.position;
}
if (other.tag == "Player2")
{
agent.destination = Player2.position;
}
*/
}
}
↧