So I’ve made a code in C# in which the AI calculates the closest player gameobject to him and following him until he is next to him then he just stares at him
Script:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.AI;
using UnityEngine.Networking;
public class EnemyController : NetworkBehaviour {
public float lookRadius = 10f;
NavMeshAgent agent;
int indexOfShortest = 0;
public GameObject[] players;
// Use this for initialization
void Start () {
Debug.Log("START_HAS_ACCOMPLISHED");
agent = GetComponent<NavMeshAgent>();
}
// Update is called once per frame
void Update () {
if (!isServer)
{
return;
}
if (NetworkManager.singleton.numPlayers >= 1)
{
players = GameObject.FindGameObjectsWithTag("Players");
float[] distance = new float[NetworkManager.singleton.numPlayers];
indexOfShortest = 0;
for (int i = 0; i < distance.Length; i++)
{
distance[i] = Vector3.Distance(players[i].transform.position, transform.position);
if (i == 0)
{
indexOfShortest = 0;
}
if (i > 0 && distance[i] <= distance[i - 1])
{
indexOfShortest = i;
}
}// Calculated distance from each player
if (distance[indexOfShortest] <= lookRadius)
{
agent.isStopped = false;
agent.SetDestination(players[indexOfShortest].transform.position); //If the player is in the radius chase
if (distance[indexOfShortest] <= agent.stoppingDistance)
{
//attack target
FaceTarget();
}
}
}
}
void FaceTarget()
{
Vector3 direction = (players[indexOfShortest].transform.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) ;
}
}
Hopefully one of you’d be able to tell me why he is not following the client
If you’re asking yourself how I made the client the closer one I just made a ramp the launches the
host miles away or anything else that collides with it…