- Home /
Photon Networking: How to spawn my players at the another places?
I'm working on multiplayer game and i'm using Photon Networking. I'm trying to spawn my players at the different places but i'cant do that. I tried to do that with their id's but i couldn't.
Here is my code:
using System.IO;
using UnityEngine;
using UnityEngine.SceneManagement;
public class PlayerNetwork : Photon.MonoBehaviour {
public static PlayerNetwork Instance;
public string PlayerName { get; private set; }
private PhotonView PhotonView;
private int PlayersInGame = 0;
public GameObject playerPrfb;
private void Awake ()
{
Instance = this;
PhotonView = GetComponent<PhotonView>();
PlayerName = "Resul" + Random.Range(1000, 9999);
PhotonNetwork.sendRate = 60;
PhotonNetwork.sendRateOnSerialize = 30;
SceneManager.sceneLoaded += OnSceneFinishedLoading;
}
private void OnSceneFinishedLoading(Scene scene, LoadSceneMode mode)
{
if(scene.name == "Multiplayer1")
{
if (PhotonNetwork.isMasterClient)
MasterLoadedGame();
else
NonMasterLoadedGame();
}
}
private void MasterLoadedGame()
{
PhotonView.RPC("RPC_LoadedGameScene", PhotonTargets.MasterClient);
PhotonView.RPC("RPC_LoadGameOthers", PhotonTargets.Others);
}
private void NonMasterLoadedGame()
{
PhotonView.RPC("RPC_LoadedGameScene", PhotonTargets.MasterClient);
}
[PunRPC]
private void RPC_LoadGameOthers()
{
PhotonNetwork.LoadLevel(18);
}
[PunRPC]
private void RPC_LoadedGameScene()
{
PlayersInGame++;
if(PlayersInGame == PhotonNetwork.playerList.Length)
{
print("All players are in the game scene ");
PhotonView.RPC("RPC_CreatePlayer", PhotonTargets.All);
}
}
[PunRPC]
private void RPC_CreatePlayer()
{
PhotonNetwork.Instantiate(Path.Combine("Prefabs", "Plyr"), new Vector2(1.34f, 4.711f), Quaternion.identity, 0);
}
}
as you can see on the RPC_CreatePlayer my all players spawning at the same place. Thanks for any help.
Answer by ChristianSimon · Apr 17, 2018 at 10:16 AM
Hi,
while editing the scene you can already place some spawn points and make them available in code. Then each client can select a spawn position to spawn his character on a certain spot.
To do this, you can use the PlayerRoomIndexing for example which applies a unique 'index' to each client in the room. You can access this value on the local client by using PlayerRoomIndexing.instance.GetRoomIndex(PhotonNetwork.player)
for example. You can map this unique index to a certain spawn point where the client can spawn his character.
Answer by asadmubashr · Feb 10, 2021 at 12:58 AM
Please brother if you found answer and have implemented this structure, can you share code... please you can save my days.
Your answer
Follow this Question
Related Questions
How to Instance GameObjects Over the Pun Network 2 Answers
Photon Network, how to spawn FPSController from Unity Standard Assets? 2 Answers
[UNET] Dynamically position NetworkStartPosition Gameobjects 2 Answers
How to modify enemy players meshs with Photon Unity Networking? 1 Answer
Photon Cloud Networking: OnPhotonSerializeView Not Firing 9 Answers