How to identify players on Photon Unity?
Hey
Im trying to make a multiplayer racing game using Photon Unity, but Im not sure how to assign unique IDs to the players. What I'm trying to do is once the players create/join their room, they don't all spawn at the same spawn point, but rather, one person spawns per spawn point.
For example heres the code for the NetworkManager currently
using UnityEngine;
using System.Collections;
public class NetworkManager : MonoBehaviour {
private const string Version = "v1.0";
public string roomname = "MultiplayerRoom";
public string playerPrefabName = "MultiplayerCar01";
public Transform spawnpoint_1;
public Transform spawnpoint_2;
public Transform spawnpoint_3;
public Transform spawnpoint_4;
public Transform spawnpoint_5;
public Transform spawnpoint_6;
public Transform spawnpoint_7;
public Transform spawnpoint_8;
void OnJoinedRoom ()
{
if (PhotonNetwork.room.playerCount == 8)
StartGame ();
}
void StartGame ()
{
PhotonNetwork.Instantiate(playerPrefabName,
spawnpoint_1.position,
spawnpoint_1.rotation,
0);
}
void Start () {
PhotonNetwork.ConnectUsingSettings(Version);
}
void OnJoinedLobby () {
RoomOptions roomOptions = new RoomOptions() { isVisible = true, maxPlayers = 8 };
PhotonNetwork.JoinOrCreateRoom(roomname, roomOptions, TypedLobby.Default);
}
}
As you can see I have 8 spawn points but my question is how do I differentiate the players so that Player 1 goes to spawn point 1, Player 2 to spawn point 2 etc.
Thanks
Answer by rohankad · Jul 28, 2015 at 12:37 PM
First of all take the spawnpoints as an array of maximum 8. Get the numbers of players connected using the PhotonNetwork.playerList. Run a for loop and set the players to their positions.
Thanks for your answer
I created this script following your advice
using UnityEngine;
using System.Collections;
public class Network$$anonymous$$anager : $$anonymous$$onoBehaviour {
private const string Version = "v1.0";
public string roomname = "$$anonymous$$ultiplayerRoom";
public string playerPrefabName = "$$anonymous$$ultiplayerCar01";
public GameObject[] spawnpoints;
public int Spawnpoint$$anonymous$$ax = PhotonNetwork.room.playerCount;
void OnJoinedRoom ()
{
if (PhotonNetwork.room.playerCount == 8)
{
StartGame ();
}
}
void StartGame ()
{
for (int i = 0; i <= Spawnpoint$$anonymous$$ax; i ++)
{
PhotonNetwork.Instantiate(playerPrefabName,
spawnpoints[i].position,
spawnpoints[i].rotation,
0);
}
}
void Start () {
PhotonNetwork.ConnectUsingSettings(Version);
}
void OnJoinedLobby () {
RoomOptions roomOptions = new RoomOptions() { isVisible = true, maxPlayers = 8 };
PhotonNetwork.JoinOrCreateRoom(roomname, roomOptions, TypedLobby.Default);
}
}
but I get the errors
Assets/$$anonymous$$ultiplayer/Network/Network$$anonymous$$anager.cs(26,58): error CS1061: Type UnityEngine.GameObject' does not contain a definition for
position' and no extension method position' of type
UnityEngine.GameObject' could be found (are you missing a using directive or an assembly reference?)
Assets/$$anonymous$$ultiplayer/Network/Network$$anonymous$$anager.cs(27,58): error CS1061: Type UnityEngine.GameObject' does not contain a definition for
rotation' and no extension method rotation' of type
UnityEngine.GameObject' could be found (are you missing a using directive or an assembly reference?)
Assets/$$anonymous$$ultiplayer/Network/Network$$anonymous$$anager.cs(25,27): error CS1502: The best overloaded method match for PhotonNetwork.Instantiate(string, UnityEngine.Vector3, UnityEngine.Quaternion, int)' has some invalid arguments Assets/$$anonymous$$ultiplayer/Network/Network$$anonymous$$anager.cs(25,27): error CS1503: Argument
#2' cannot convert object' expression to type
UnityEngine.Vector3'
I don't think that these errors come with the instantiate coding because I used the script before without error.
Do you have any idea how to fix it?
Thanks
spawnpoints[i].transform.position,
spawnpoints[i].transform.rotation,
my player spawns on the same position with this code:
void OnJoinedRoom(){ if (PhotonNetwork.room.playerCount == 1) { GameObject meinSpieler = PhotonNetwork.Instantiate ("Spieler", new Vector3 (-0.95f, 5f, 18f), Quaternion.identity, 0); meinSpieler.transform.localRotation = Quaternion.Euler (0, 180, 0); } if (PhotonNetwork.room.playerCount == 2) { GameObject meinSpieler = PhotonNetwork.Instantiate ("Spieler", new Vector3 (-0.95f, 5f, -18f), Quaternion.identity, 0); } }
if i use your code, just 1 player is spawning. Why is this?
i have an error with the SpawnPoint$$anonymous$$ax. It says You are not allowed to call this function when declaring a variable
Answer by michaelheise61 · Jan 14, 2021 at 11:05 AM
spawnpoint[PhotonNetwork.player.ID] as position to instantiate your prefab so spawnpoint should be a vector3 array. Its one line of code. As rotation you could use identity or just make another quaternion array with spawnrotation. That is literally one line of code. excludining defining the arrays.
Your answer
Follow this Question
Related Questions
Unity Photon Next Step? 0 Answers
Unity Photon Next Step? 0 Answers
How to create several spawnpoints without spawning at the same place in Photon 2 Answers
Unity Photon doesn't Spawn Player 0 Answers