Skip to content

Instantly share code, notes, and snippets.

@nickyeh97
Last active January 31, 2018 07:26
Show Gist options
  • Save nickyeh97/fe42b35cb46669d02886669a93dcbedb to your computer and use it in GitHub Desktop.
Save nickyeh97/fe42b35cb46669d02886669a93dcbedb to your computer and use it in GitHub Desktop.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class SpawnRandow : Photon.MonoBehaviour
{
[SerializeField]
private GameObject[] TeamA;
[SerializeField]
private GameObject[] TeamB;
private GameObject spwanPoint;
//在玩家看到畫面前執行
private void Awake()
{
//生成點列表
TeamA = GameObject.FindGameObjectsWithTag("SpawnA");
TeamB = GameObject.FindGameObjectsWithTag("SpawnB");
//不重複隨機數組
int[] randomNumberA = GetRandomSequence(TeamA.Length, TeamA.Length);
int[] randomNumberB = GetRandomSequence(TeamB.Length, TeamB.Length);
// How to get Player.order??? --> (PhotonManager.instance.players[index].ID)
for (int index = 0; index < PhotonManager.instance.players.Length; index++)
{
PunTeams.Team team = PhotonManager.instance.players[index].GetTeam();
//print(PhotonManager.instance.players[index].NickName + " is Team-" + PhotonManager.instance.players[index].GetTeam());
//PhotonNetwork.player.ID 從 1 開始
int playerID = PhotonManager.instance.players[index].ID;
if (playerID > 16)
{
playerID -= 16;
}
//隨機生成點by players.ID + player.getTeam()
if (team == PunTeams.Team.red)
{
spwanPoint = TeamA[randomNumberA[(playerID + 1) / 2 - 1]];
}
if (team == PunTeams.Team.blue)
{
spwanPoint = TeamB[randomNumberB[(playerID) / 2 - 1]];
}
if (photonView.isMine)
transform.position = spwanPoint.transform.position;
}
}
public int[] GetRandomSequence(int total, int n)
{
//建數值表
int[] sequence = new int[total];
//建亂數表
int[] output = new int[n];
for (int i = 0; i < total; i++)
{
sequence[i] = i;
}
int end = total - 1;
for (int i = 0; i < n; i++)
{
//每隨機一次,區間-1
int num = Random.Range(0, end + 1);
output[i] = sequence[num];
sequence[num] = sequence[end];
end--;
//第一次執行:1,2,3,4,5 取到2
//下次隨機區間變为1,5,3,4;
}
return output;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment