How to create multi-user at fixed resolution in unity using photon?
I am using the Photon networking module for a multiplayer game. in the following code to create a symbol, I want the player that joins to be created at a fixed point and then random. I am new to this and I tried to edit it with a fixed position of the gameObject on the button click event but was unable to do so. Here is the code -
using UnityEngine;
public class CharacterInstantiation : OnJoinedInstantiate {
public delegate void OnCharacterInstantiated(GameObject character);
public static event OnCharacterInstantiated CharacterInstantiated;
public new void OnJoinedRoom() {
if (this.PrefabsToInstantiate != null) {
GameObject o = PrefabsToInstantiate[(PhotonNetwork.player.ID - 1) % 4];
//Debug.Log("Instantiating: " + o.name);
Vector3 spawnPos = Vector3.zero;
if (this.SpawnPosition != null) {
spawnPos = this.SpawnPosition.position;
}
Vector3 random = Random.insideUnitSphere;
random = this.PositionOffset * random.normalized;
spawnPos += random;
spawnPos.y = 0;
Camera.main.transform.position += spawnPos;
o = PhotonNetwork.Instantiate(o.name, spawnPos, Quaternion.identity, 0);
if (CharacterInstantiated != null) {
CharacterInstantiated(o);
}
}
}
}
this code is in the plugin test scene. I just want the game players to become fixed like spawnpoint [0], spawnpoint [1], etc. Thanks in advance for your help.
and here is the code to create the prefile in the plugin -
public class OnJoinedInstantiate : MonoBehaviour
{
public Transform SpawnPosition;
public float PositionOffset = 2.0f;
public GameObject[] PrefabsToInstantiate;
public void OnJoinedRoom()
{
if (this.PrefabsToInstantiate != null)
{
foreach (GameObject o in this.PrefabsToInstantiate)
{
Debug.Log("Instantiating: " + o.name);
Vector3 spawnPos = Vector3.up;
if (this.SpawnPosition != null)
{
spawnPos = this.SpawnPosition.position;
}
Vector3 random = Random.insideUnitSphere;
random.y = 0;
random = random.normalized;
Vector3 itempos = spawnPos + this.PositionOffset * random;
PhotonNetwork.Instantiate(o.name, itempos, Quaternion.identity, 0);
}
}
}
}
+3
RingR89
source
to share
1 answer
If you want to create different spawn points, you must change your script to:
using UnityEngine;
public class CharacterInstantiation : OnJoinedInstantiate {
public delegate void OnCharacterInstantiated(GameObject character);
public static event OnCharacterInstantiated CharacterInstantiated;
public int counter = 0;
public Vector3[] spawnPositions;
public new void OnJoinedRoom() {
if (this.PrefabsToInstantiate != null) {
GameObject o = PrefabsToInstantiate[(PhotonNetwork.player.ID - 1) % 4];
//Debug.Log("Instantiating: " + o.name);
Vector3 spawnPos = Vector3.zero;
if (this.SpawnPosition != null) {
spawnPos = spawnPositions[counter];
}
Vector3 random = Random.insideUnitSphere;
random = this.PositionOffset * random.normalized;
spawnPos += random;
spawnPos.y = 0;
Camera.main.transform.position += spawnPos;
o = PhotonNetwork.Instantiate(o.name, spawnPos, Quaternion.identity, 0);
if (CharacterInstantiated != null) {
CharacterInstantiated(o);
counter++;
}
}
}
}
You just need to provide values ββfor spawnPositions
.
+1
Hristo
source
to share