Search Unity

Question AI.SetDestination not working in this specific block of code

Discussion in 'Navigation' started by IncrediblyWalrus, Jun 25, 2022.

  1. IncrediblyWalrus

    IncrediblyWalrus

    Joined:
    Jul 22, 2019
    Posts:
    46
    Hi there,

    I've recently been trying to have my AI go to a player with the highest fear factor (this is a multiplayer game and is using PUN 2 if that is of matter).

    Here is the snippet for this:

    Code (CSharp):
    1.         PlayerManager whoIsHighest = null;
    2.         var AllPlayers = FindObjectsOfType<PlayerManager>();
    3.         float highestFear = 100;
    4.        
    5.         foreach( var p in AllPlayers)
    6.         {
    7.             if (p.currentFear >= highestFear || (whoIsHighest == null))
    8.             {
    9.                 whoIsHighest = p;
    10.                 highestFear = p.currentFear;
    11.             }
    12.         }
    13.        
    14.         if (whoIsHighest != theHighest) theHighest = whoIsHighest;
    15.  
    16.         if (theHighest != null && theHighest.currentFear >= 50)
    17.         {
    18.             Debug.Log(whoIsHighest.name);
    19.             Debug.Log("Testing");
    20.             AI.SetDestination(theHighest.transform.position);
    21.         }
    It's grabbing all players that have the PlayerManager script.

    The main issue, I believe, is the SetDestination line for the AI. The debug logs show correctly for even the second client if they reach the value of 50, however, the AI will only target the first client.

    I am using PUN 2, which the first client is obviously the master client, but surely that shouldn't be the issue on why the AI isn't targeting anyone but the first client because the other things (the logs) in that code block show for other clients if they reach 50 or more.

    Is there something I'm doing wrong here with the block of code? If you need more of an explanation please let me know.