PhotonNetwork.Instantiate Players as Child of empty game object...

The whole answer can be found below.

Please note: The Photon forum is closed permanently. After many dedicated years of service we have made the decision to retire our forum and switch to read-only: we've saved the best to last! And we offer you support through these channels:

Try Our
Documentation

Please check if you can find an answer in our extensive documentation on PUN.

Join Us
on Discord

Meet and talk to our staff and the entire Photon-Community via Discord.

Read More on
Stack Overflow

Find more information on Stack Overflow (for Circle members only).

Write Us
an E-Mail

Feel free to send your question directly to our developers.

PhotonNetwork.Instantiate Players as Child of empty game object...

DiGiaComTech
2021-11-22 22:29:22

I'm using the standard Unity method to instantiate new items into a parent via the GameObject.Transorm.Parent method. I am using with the Photon method to instantiate but it only childs the first player into the GameObject with all other players being placed into the root (I think this is because Photon takes over after the first instantiation).

Here is my base code with the PlayerFolder being set in the inspector to an empty GameOject in the root of the hierarchy:

public Transform PlayerFolder;

// Instantiate player

GameObject NewPlayer = PhotonNetwork.Instantiate(PlayerPrefab.name, StartPosition, Quaternion.identity);

NewPlayer.transform.parent = PlayerFolder;

NewPlayer.gameObject.name = string.Format("{0}: {1}", NewPlayer.gameObject.name, PhotonNetwork.NickName);

How do I tell Photon to instantiate ALL players as children of the of the same parent object?

Or if this is not currently possible, then how do I submit this as a new feature request?

P.S. I'm doing this because I'm NEW to Photon and I can't find a way of getting the transform.position of all players within  PhotonNetwork.LocalPlayer or PhotonNetwork.PlayerListOthers.

Comments

maxclark
2021-11-23 00:21:09

hello, it seems to me that on the computer that executes this code, the player will be childed correctly. however, on other computers, they only receive a message to create a player and nothing else (because PhotonNetwork.Instantiate will call the instantiation on all connected machines).

If you want everyone to take their networked player object and child it to something, you'll need to send a network message, probably best to use an RPC, or you can try using instantiationData, which you can pass in as an argument to PhotonNetwork.Instantiate.

DiGiaComTech
2021-11-23 00:41:58

What I'm really trying to do on each client is rotate the name/health banner of non clients towards the client player's camera (using Unity's LookAt function for example). So I need to do this in the update method of each player object in the hierarchy that is not my player object ... and that probably needs to be done after Photon repositions them.

I suppose I could build and manage a dictionary of player GameObjects via the Photon OnJoin & OnLeave events and reference that vs. trying to FIND specific things in the Unity scene hierarchy :(

But I think I understand you comment ... my client code instantiates me, but joining players are instantiated in my client by Photon's back end code. Anyway, just starting to learn Photon ... got a long way to go ;)

Back to top