I have been using Photon unity networking plugin for multiplayer. in the following code for character instantiation I want to spawn the player who joins to be spawned at a fixed point then random. I am new at this and I tried to edit it by giving fixed gameObject position at a 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 test scene with the plugin. Just want to spawn the joining players are fixed point like spawnpoint[0], spawnpoint[1] and so on. Thanks in advance for the help.
and here is the code for prefab instantiate 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);
}
}
}
}