Enemy AI for Multiplayer (using NavMeshAgent) unity 5.6.5f1
So, I'm trying to make an enemy ai for a multiplyer game that can keep an array of all players, and once one of the players is in its lookRadius, it keeps it in a target variable and uses a navmeshagent to follow it. I have a rough idea of how to do it, but i keep on getting errors. here is my code: using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.AI; public class EnemyAI : MonoBehaviour { public float lookRadius = 10f; Transform target; public bool Activate = false; public bool Activated = false; NavMeshAgent agent; // Use this for initialization void Start () { agent = GetComponent<NavMeshAgent> (); } // Update is called once per frame void Update () { if (Activate == true) { float distance = Vector3.Distance (target.position, transform.position); if (distance <= lookRadius) { agent.SetDestination (target.position); if (distance <= agent.stoppingDistance) { FaceTarget (); } } } } void OnDrawGizmosSelected () { Gizmos.color = Color.red; Gizmos.DrawWireSphere (transform.position, lookRadius); } public void FindTarget() { if (Activate == true) { if (Activated != true) { target = GameObject.FindGameObjectWithTag ("Player").transform; } } } void FaceTarget() { Vector3 direction = (target.position - transform.position).normalized; Quaternion lookRotation = Quaternion.LookRotation (new Vector3 (direction.x, 0, direction.z)); transform.rotation = Quaternion.Slerp(transform.rotation, lookRotation, Time.deltaTime * 5f); } }
the player would set the Activate and Activated variables to true on it's start method.
Your answer
Follow this Question
Related Questions
What's wrong with my script ? 0 Answers
how to connect WEBRTC in unity ? 2 Answers
How do I create a disconnect button in a scene [UNET] 0 Answers
[uNet] Multiple Spawnpoints with Selection 0 Answers
Networking With Colors Unity 5 2 Answers