3

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);
        }
    }
  }
}
7
  • What do you mean you are unable to edit it? You can't open the script? Commented Apr 29, 2017 at 15:11
  • Yes I can edit the script and I have corrected the question :) Commented Apr 29, 2017 at 16:08
  • Well you could pass the position when you are Instantiating the object. In your case, replace spawnPos with new Vector3(0, 0, 0) or whatever the spawnpoint. Commented Apr 29, 2017 at 16:16
  • You have to change your first script not the second, then it should work. Commented May 1, 2017 at 5:35
  • 1
    in the first one all players joining instantiate at same location with an offset. the instantiating objects are in a list player.ID. when I apply the method I am only able to change the location spawn point where every player would get spawned not differently. Commented May 1, 2017 at 6:42

1 Answer 1

2

If you want to make different spawn points you should 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 give values for spawnPositions.

Sign up to request clarification or add additional context in comments.

10 Comments

thanks for your help I tried and still the players joining are instantiating on the same location as the first one.
Did you add at least two different points for my spawnPositions?
yes I added 3 spawnpoints the first user who started the server got on right location but others got on the same as the first
Could you add a break point and test if the location is even changed. Or even if the OnJoinedRoom() is ever called.
this might take longer so for clarity if you can, then please import photon voice asset in a new project and go to demo voice scene and can see it yourself. | I will mark the answer for your help Thanks :) | will wait for you if you do the same
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.