0
votes

I am trying to make an RTS game using Photon Networking For Unity. What I am trying to do is depending on the players in the specific room (2 in this example) all the players spawn at a different location. What I am having trouble is making a system that spawns the players at different locations and not at the same place when the game starts and PhotonNetwork.Instantiate is called. How can I use SpawnPositions With A list to check if a player is already spawned at the given location if so then it spawns at the next one I hope I covered everything if you need to know anything else just ask.

        private GameObject playerPrefab; // Players Prefab
        [SerializeField]
        public Transform[] SpawnPositions; // 2 Spawn Positions in The unity hierarchy

        public List<Vector3> spawnPoints = new List<Vector3>(); // List of spawnpoints to add to

        void Start()
        {

        PhotonNetwork.Instantiate(playerPrefab.name, [what to do here], Quaternion.Euler(0, 0, 0));

        }
2

2 Answers

1
votes

Edit 2: using Transform[] SpawnPositions

private int spawnIndex;
...
void CreatePlayerObject()
    {
        if(spawnIndex >= SpawnPositions.length) spawnIndex = 0;
        Vector3 position = SpawnPositions [spawnIndex].transform.position;

        GameObject newPlayerObject = Instantiate(player, position, Quaternion.identity);
        spawnIndex++;
    }
...

end edit 2

You can make spawn points. I made the spawn points automatically find the right height. If you don't need auto height, you can use an empty MonoBehaviour or even use a tag. You can make many GameObjects in different positions with this script on them.

...
public class spawnPoint : MonoBehaviour {
    public bool autoHeight = true;
    public LayerMask putOnTopOfThese;
    // Use this for initialization
    void Start () {
        RaycastHit hit;
        Physics.Raycast (transform.position + Vector3.up * 100, -Vector3.up, out hit, 200, putOnTopOfThese);
        transform.position = hit.point + Vector3.up;
    }
}

To make every player spawn in a different position, you can cycle through the spawn points. Keep in mind that if you use a tag for spawn points instead of a MonoBehaviour, you would need to use GameObject.FindObjectsWithTag instead of FindObjectsOfType

For spawning in different positions:

private int spawnIndex;
...
void CreatePlayerObject()
    {
        spawnPoint[] sp = GameObject.FindObjectsOfType<spawnPoint> ();
        if(spawnIndex >= sp.length) spawnIndex = 0;
        Vector3 position = sp [spawnIndex].transform.position;

        GameObject newPlayerObject = Instantiate(player, position, Quaternion.identity);
        spawnIndex++;
    }
...

For random spawning:

...
void CreatePlayerObject()
    {
        spawnPoint[] sp = GameObject.FindObjectsOfType<spawnPoint> ();
        int chosen = Random.Range (0, sp.Length);
        Vector3 position = sp [chosen].transform.position;

        GameObject newPlayerObject = Instantiate(player, position, Quaternion.identity);
    }
...

Edit: clarity

0
votes

Thank you bazilbaachu(https://answers.unity.com/users/1127669/bazilbaachu.html).

I am posting my entire workflow of instantiating players at different positions:

Initially checking whether we are master client or not:

Then obtain list of players and send rpc to each of them individually on positions to spawn them.You can store an array of positions and send index in rpc.

Here is the code:

photonView = PhotonView.Get(this);
    if(PhotonNetwork.IsMasterClient)
    {
        foreach(Player pl in PhotonNetwork.PlayerList)
        {
            //Debug.Log(pl);
            photonView.RPC("InstantiationPlayer",pl,index);
           
            index++;
        }
    }

 [PunRPC]
    void InstantiationPlayer(int index)
    {
        

PhotonNetwork.Instantiate(Playerprefabs[value].name,SpawnPoints[index].transform.position, Quaternion.identity, 0); }

Thank you.