How to get everyone's position on one user's end in real-time?

Options
Greetings,

I want to implement this feature for my project: each player has a "zone" of radius 1 meter. if any player in the room comes into my zone, then some event will be triggered by calling some methods. Now my question is, how do I collect the position from all players in the room to compute the distance? (I don't want to use colliders because I don't want to interact with other objects that have colliders.)

Currently I'm using PhotonView and PhotonTransformView to synchronize the positions of players, and it works well. I have a global script attached to a scene object that finds each player instance with GameObject.FindGameObjectsWithTag() in the Update() function, in case some player joins the room and the player amount will be changed. Following that, a for loop is implemented for checking the distances between the local player and all other players:

players = GameObject.FindGameObjectsWithTag("Player");
for (int i = 0; i < players.Length; i++)
{
PhotonView pv = players.GetComponent<PhotonView>();
if (pv.IsMine)
continue;
if (Vector3.Distance(localPlayer.transform.position, players.transform.position) < 1)
{
// do something;
}
}

However, sometimes an error pops up indicating the index is out of range. My guess is that some remote players leave the room while on my end the script is running at the beginning of the for loop, but I don't understand that how could "players" be changed since it's loaded.

Otherwise do you have any suggestion for implementing this "zone" feature? Thanks in advance!

For reference, the Unity version I'm using is 2019.4.11, and the PUN version is 2.22.2.

Comments

  • Danhua
    Options
    Sorry to reply late. This issue seems to be caused by some other bugs.