Search Unity

AI follows only host but not client

Discussion in 'Navigation' started by eyalandron, Jul 11, 2018.

  1. eyalandron

    eyalandron

    Joined:
    Jan 10, 2018
    Posts:
    3
    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:

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.AI;
    5. using UnityEngine.Networking;
    6.  
    7. public class EnemyController : NetworkBehaviour {
    8.  
    9.     public float lookRadius = 10f;
    10.     NavMeshAgent agent;
    11.     int indexOfShortest = 0;
    12.     public GameObject[] players;
    13.     // Use this for initialization
    14.     void Start () {
    15.         Debug.Log("START_HAS_ACCOMPLISHED");
    16.         agent = GetComponent<NavMeshAgent>();
    17.     }
    18.    
    19.     // Update is called once per frame
    20.     void Update () {
    21.  
    22.         if (!isServer)
    23.         {
    24.             return;
    25.         }
    26.         if (NetworkManager.singleton.numPlayers >= 1)
    27.         {
    28.             players = GameObject.FindGameObjectsWithTag("Players");
    29.             float[] distance = new float[NetworkManager.singleton.numPlayers];
    30.             indexOfShortest = 0;
    31.             for (int i = 0; i < distance.Length; i++)
    32.             {
    33.                 distance[i] = Vector3.Distance(players[i].transform.position, transform.position);
    34.                 if (i == 0)
    35.                 {
    36.                     indexOfShortest = 0;
    37.                 }
    38.                 if (i > 0 && distance[i] <= distance[i - 1])
    39.                 {
    40.                     indexOfShortest = i;
    41.                 }
    42.             }// Calculated distance from each player
    43.             if (distance[indexOfShortest] <= lookRadius)
    44.             {
    45.                 agent.isStopped = false;
    46.                 agent.SetDestination(players[indexOfShortest].transform.position); //If the player is in the radius chase
    47.                 if (distance[indexOfShortest] <= agent.stoppingDistance)
    48.                 {
    49.                     //attack target
    50.                     FaceTarget();
    51.                 }
    52.             }
    53.  
    54.         }
    55.        
    56.     }
    57.  
    58.  
    59.     void FaceTarget()
    60.     {
    61.         Vector3 direction = (players[indexOfShortest].transform.position - transform.position).normalized;
    62.         Quaternion lookRotation = Quaternion.LookRotation(new Vector3(direction.x, 0, direction.z));
    63.         transform.rotation = Quaternion.Slerp(transform.rotation,lookRotation,Time.deltaTime * 5f) ;
    64.     }
    65.  
    66. }
    67.  
    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..
     
  2. newjerseyrunner

    newjerseyrunner

    Joined:
    Jul 20, 2017
    Posts:
    966
    Code (csharp):
    1. if (i > 0 && distance <= distance[i - 1])

    That line is comparing the current distance with the distance of the previous entry. I think you want to compare it to the one at distance[indexOfShortest].

    Also, why keep an array of the distances? all you need to know at the end is what the shortest distance was and which index it was. Use a shortestDistanceSoFar variable or something.