Hi, can anyone help with AI targeting please?
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 gameobjectpublic 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.1 -
Thank you, you are life saver.0
-
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?
0