0

I've been trying to work on a targeting system with a highest property value for all players within a game. Right now my code looks like this:

    PlayerManager whoIsHighest = null;
    var AllPlayers = FindObjectsOfType<PlayerManager>();
    float highestFear = 100;
    
    foreach( var p in AllPlayers)
    {
        if (p.currentFear >= highestFear || (whoIsHighest == null))
        {
            whoIsHighest = p;
            highestFear = p.currentFear;
        }
    }
    
    if (whoIsHighest != theHighest) theHighest = whoIsHighest;

    if (theHighest != null && theHighest.currentFear >= 50)
    {
        Debug.Log(whoIsHighest.name);
        Debug.Log("Testing");
        AI.SetDestination(theHighest.transform.position);
    }

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 my code? If you need more of an explanation please let me know.

Reposted this with hopefully better tags and responses.

WizardLew
  • 23
  • 2
  • You can only go to one place on one object. If you have the same code on multiple objects chances they make the same decision. You would need to mark it as targeted and exclude it from the picking – BugFinder Jun 26 '22 at 17:19
  • @BugFinder I assumed that is what I was doing with the array, where I was marking the player as the highest valued person and then using " if (whoIsHighest != theHighest) theHighest = whoIsHighest" I'm slightly new to Unity so how would I go about doing what you're mentioning in code terms? Also I appreciate your response on this, it's been driving me mad trying to get one. – WizardLew Jun 26 '22 at 17:26
  • Im on a phone. So no. Not writing code. You need either some form of aitarget manager where it tracks all possible targets and handles if its targeted or not for each available killer assign 1 target to that killer and mark the target targeted. Repeat once a killer has no target. See if there’s another – BugFinder Jun 26 '22 at 18:39
  • @BugFinder I'm going to assume you mean that to have ONE script file, with this one singular code block running inside of it. That is basically what is happening, no? Unless I'm assuming different, please correct if wrong. – WizardLew Jun 28 '22 at 13:26

0 Answers0