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);
        }
    }
  }
}
RingR89
  • 89
  • 2
  • 13
  • What do you mean you are unable to edit it? You can't open the script? – Hristo Apr 29 '17 at 15:11
  • Yes I can edit the script and I have corrected the question :) – RingR89 Apr 29 '17 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. – Hristo Apr 29 '17 at 16:16
  • You have to change your first script not the second, then it should work. – Hristo May 01 '17 at 05: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. – RingR89 May 01 '17 at 06:42
  • But you know when a character spawns, so you can change the position for the next character, and then the next and so on.. – Hristo May 01 '17 at 06:44
  • can you give some example using the script :) – RingR89 May 01 '17 at 06:51

1 Answers1

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.

Hristo
  • 1,805
  • 12
  • 21
  • thanks for your help I tried and still the players joining are instantiating on the same location as the first one. – RingR89 May 01 '17 at 07:31
  • Did you add at least two different points for my `spawnPositions`? – Hristo May 01 '17 at 07:37
  • 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 – RingR89 May 01 '17 at 07:41
  • Could you add a break point and test if the location is even changed. Or even if the OnJoinedRoom() is ever called. – Hristo May 01 '17 at 08:07
  • added break points, the OnJoinedRoom() is called and the location for user 1 changes – RingR89 May 01 '17 at 08:50
  • 2
    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 – RingR89 May 01 '17 at 09:01
  • This should do it `spawnPos = spawnPositions[counter];` Look at my update. – Hristo May 01 '17 at 09:40
  • didn't work same result as before user1 on the required location but others also come on the same location – RingR89 May 01 '17 at 10:33
  • did you use the same demo scene – RingR89 May 01 '17 at 10:35
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/143081/discussion-between-hristo-and-derek89). – Hristo May 01 '17 at 10:35