Sync random weapon spawn point for all players
Options
I have this Spawn point that spawns a random weapon or powerup every X seconds, every time someone takes the weapon the spawn point counts X seconds (I have it set for 20 seconds) and spawns another weapon or powerup
how can I sync it for all the players?
This is the code for the spawn point:
how can I sync it for all the players?
This is the code for the spawn point:
using UnityEngine; using System.Collections; using System.Collections.Generic; public class OnlinePowerUpSpawn : Photon.MonoBehaviour { public List<PowerUp> PowerUps; private int PowerUpIndex; public float NextPowerUpTimer; private bool PowerUpIsAvailable; // Use this for initialization void Start () { foreach (PowerUp _PowerUp in PowerUps){ _PowerUp.PowerUpObject.SetActive(false); } StartCoroutine(SelectNextPowerUp()); } // Update is called once per frame void Update () { } void OnTriggerEnter(Collider Other){ if (Other.tag == "Player" && PowerUpIsAvailable){ AudioManager.Instance.PlayAudioOneShot(AudioManager.Instance.PickUp, AudioManager.Instance.FXVolume); if (PowerUps[PowerUpIndex].IsWeapon){ Other.SendMessage("ChangeWeapon", PowerUps[PowerUpIndex].Value); } else if (!PowerUps[PowerUpIndex].IsWeapon){ Other.SendMessage("CureFunc", PowerUps[PowerUpIndex].Value); } StartCoroutine(SelectNextPowerUp()); PowerUps[PowerUpIndex].PowerUpObject.SetActive(false); PowerUpIsAvailable = false; } } IEnumerator SelectNextPowerUp(){ yield return new WaitForSeconds(NextPowerUpTimer); PowerUpIndex = Random.Range(0, PowerUps.Count); PowerUps[PowerUpIndex].PowerUpObject.SetActive(true); PowerUpIsAvailable = true; } }
0
Comments
-
I really need help with this, how can I make it so when it spawns a random weapon it spawns the same weapon for all the players.0
-
You need to run spawn point logic on one client (usually master). It does not make sense use timers since master can be switched to another client. So just check next spawn time in Update() on master.
Of course you need to synchronize next spawn time between clients with OnPhotonSerializeView or RPC.
Handle collision with spawn point on collided player's client and send RPC to master to reset spawn point.0 -
vadim wrote:You need to run spawn point logic on one client (usually master). It does not make sense use timers since master can be switched to another client. So just check next spawn time in Update() on master.
Of course you need to synchronize next spawn time between clients with OnPhotonSerializeView or RPC.
Handle collision with spawn point on collided player's client and send RPC to master to reset spawn point.
I see the logic of what you are saying, but how do I run the spawn points only on the master? Sorry Its my first using this package :oops:
EDIT: I think I found out how, using PhotonNetwork.isMasterClient, right?0 -
Yes. Check PhotonNetwork.isMasterClient before executing logic in Update().0
-
Im having problems with this again, it works on the Master client but on the rest of the player it only works with the first weapon of the list, if the random number is 1 or more the code on the collision detection doesnt work... I have been stuck on this for days this is driving me nuts :? :? :?
[code2=csharp]using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class OnlinePowerUpSpawn : Photon.MonoBehaviour {
public List<PowerUp> PowerUps;
private int PowerUpIndex;
public float NextPowerUpTimer;
private float HoldPowerUpTimer;
private bool PowerUpIsAvailable;
// Use this for initialization
void Start () {
HoldPowerUpTimer = NextPowerUpTimer;
foreach (PowerUp _PowerUp in PowerUps){
_PowerUp.PowerUpObject.SetActive(false);
}
PowerUpIsAvailable = false;
}
// Update is called once per frame
void Update () {
if (PhotonNetwork.isMasterClient && !PowerUpIsAvailable){
NextPowerUpTimer -= Time.deltaTime;
if (NextPowerUpTimer <= 0){
PowerUpIndex = Random.Range(0, PowerUps.Count);
PowerUps[PowerUpIndex].PowerUpObject.SetActive(true);
PowerUpIsAvailable = true;
photonView.RPC("SyncWeapons", PhotonTargets.OthersBuffered, PowerUpIndex);
NextPowerUpTimer = HoldPowerUpTimer;
}
}
}
void OnTriggerEnter(Collider Other){
if (Other.tag == "Player" && PowerUpIsAvailable){
AudioManager.Instance.PlayAudioOneShot(AudioManager.Instance.PickUp, AudioManager.Instance.FXVolume);
if (PowerUps[PowerUpIndex].IsWeapon){
Other.SendMessage("ChangeWeapon", PowerUps[PowerUpIndex].Value);
}
if (!PowerUps[PowerUpIndex].IsWeapon){
Other.SendMessage("CureFunc", PowerUps[PowerUpIndex].Value);
}
photonView.RPC("ActivateCorutine", PhotonTargets.MasterClient);
PowerUps[PowerUpIndex].PowerUpObject.SetActive(false);
PowerUpIsAvailable = false;
}
}
[RPC]
private void ActivateCorutine(){
PowerUpIsAvailable = false;
}
[RPC]
private void SyncWeapons(int tempIndex){
PowerUps[tempIndex].PowerUpObject.SetActive(true);
PowerUpIsAvailable = true;
}
}[/code2]0 -
Looks like PowerUpIndex property is never set (always = 0) on clients other than master.0