Hi, can anyone help with AI targeting please?

So I'm new to pun and having trouble using Player to get an array of all player so my AI can pick the closest target for example public Player[] players; then using players = FindObjectsOfType<Player>() I get there is no implicit reference conversion from photon.realtime.player to unityengine.gameobject. I want to return all players in room as a gameobject or transform but im stumped any suggestions or help would be much appreciated.

Comments

  • Hi,
    you can either you some of Unity's magic methods.
    https://docs.unity3d.com/ScriptReference/Object.FindObjectOfType.html
    https://docs.unity3d.com/ScriptReference/GameObject.FindGameObjectsWithTag.html

    Or if you want to use Photons Player you have to some more steps.
    Before that Photons Player class is no Monobehaviour therefor there is no way to cast it to a GameObject.
    It stores specific data from Clients.
    But a Player can store a tagObject.
    https://doc-api.photonengine.com/en/pun/v2/class_photon_1_1_realtime_1_1_player.html#a8908e94beb8d1fb203fef2848c212aa6

    Lets say you got an Script thats on all your player gameobjects. In this script you can use the Start method like.
    private void Start()
    {
    	PhotonNetwork.LocalPlayer.TagObject = gameObject;
    }
    

    and forAi to get all valid player gameobject
    public void DoSomethingWithPlayerObjects()
    		{
    			var listOfValidPlayerObjects = new List<GameObject>();
    			foreach (var photonPlayer in PhotonNetwork.PlayerList)
    			{
    				var localPlayerObject = (GameObject) photonPlayer.TagObject;
    
    				listOfValidPlayerObjects.Add(localPlayerObject);
    			}
    		}
    

    Use this as an Example, do not just copy and paste it.
  • Thank you, you are life saver.
  • Can anyone help me? I'm having a really weird issue with this, ok so in my player script I have this in start:

    PhotonNetwork.LocalPlayer.TagObject = gameObject;

    and In my enemy AI script (Turret) I have this

    public List<GameObject> players;

     public void Start()

      {

        FindPlayers();

        FindClosestPlayer();

      }

    public void FindPlayers()

      {

        foreach (var photonPlayer in PhotonNetwork.PlayerList)

        {

          var localPlayerObject = (GameObject)photonPlayer.TagObject;


          players.Add(localPlayerObject);

          Debug.Log(photonPlayer.NickName);

        }

      }

     void FindClosestPlayer()

      {

        float distanceToClosestPlayer = Mathf.Infinity;

        GameObject ClosestPlayer = null;

        foreach(GameObject photonPlayer in players)

        {

          float distanceToPlayer = (photonPlayer.transform.position - this.transform.position).sqrMagnitude;

          if(distanceToPlayer < distanceToClosestPlayer)

          {

            distanceToClosestPlayer = distanceToPlayer;

            ClosestPlayer = photonPlayer;

            if(distanceToPlayer <= range)

            {

              target = closestPlayer.transform;

            }

          }

        }

      }

    The problem is that the commands both get called just fine, but in the inspector, I can see that only the local player tag object got added on either instance of the turret, meanwhile, the other player's tag object did add to the list count, but the actual object didn't get added (It just says empty gameobject). Does anyone know how to fix this? Or is there a better way to go about it?