I am making a FPS Shooter game with the help of PUN (learning it). Now i have made a script “WeaponManager” where i get the Input and sending a RPC (Photon View is on the Weapon GameObject, which is a Child of the PlayerController GameObject) to change the Weapon. My problem is, that if anyone wants to change his weapon, for example scrolls up, then everyone’s weapon get changed. I know where the problem is, but i do not know how to solve it. I want that only “my” weapon gets changed, but everyone sees is.
void Update () {
Debug.Log (currentWeaponName);
if (Input.GetAxis ("Mouse ScrollWheel") < 0) {
if (currentWeapon + 1 <= maxWeapons) {
currentWeapon++;
} else {
currentWeapon = 0;
}
GetComponent<PhotonView>().RPC("SelectWeapon", PhotonTargets.All, currentWeapon);
} else if (Input.GetAxis ("Mouse ScrollWheel") > 0) {
if (currentWeapon - 1 >= 0) {
currentWeapon--;
} else {
currentWeapon = maxWeapons;
}
GetComponent<PhotonView>().RPC("SelectWeapon", PhotonTargets.All, currentWeapon);
}
if (Input.GetKeyDown (KeyCode.Alpha1)) {
currentWeapon = 0;
GetComponent<PhotonView>().RPC("SelectWeapon", PhotonTargets.All, currentWeapon);
}
if (Input.GetKeyDown (KeyCode.Alpha2)) {
currentWeapon = 1;
GetComponent<PhotonView>().RPC("SelectWeapon", PhotonTargets.All, currentWeapon);
}
if (Input.GetKeyDown (KeyCode.Alpha3)) {
currentWeapon = 2;
GetComponent<PhotonView>().RPC("SelectWeapon", PhotonTargets.All, currentWeapon);
}
if (Input.GetKeyDown (KeyCode.Alpha4)) {
currentWeapon = 3;
GetComponent<PhotonView>().RPC("SelectWeapon", PhotonTargets.All, currentWeapon);
}
if (currentWeapon == maxWeapons + 1) {
currentWeapon = 0;
}
if (currentWeapon == -1) {
currentWeapon = maxWeapons;
}
}
[RPC]
void SelectWeapon(int index){
for (int i = 0; i < transform.childCount; i++) {
if(i == index){
transform.GetChild(i).gameObject.SetActive(true);
currentWeaponName = transform.GetChild(i).gameObject.name;
}
else{
transform.GetChild(i).gameObject.SetActive(false);
}
}
}
The Weapons GameObject (on which the PhotonView and the WeaponManager Script is applied) has four childs (for four Weapons) which I activate / deactivate in the Select Weapon function.