- Home /
PhotonNetwork Switching Weapons (network sync)
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.
hey did you ever get this fixed? I have EXACTLY the same problem!
Answer by stoneproductions · Apr 03, 2015 at 12:21 PM
I actually figured it out! Sorry that I did not post it already. You have to set the WeaponManager Script Component to active only for "your" player. Then it works perfectly fine. So just set it to false in the prefab and activate it when you spawn your player.
Answer by Barry100 · Dec 14, 2014 at 10:04 PM
if anyone reads this.. i thin the problem is that when the reciever gets the RPC that it dosnt know what guy needs his weapon changed so as it is never told who to change.. so it changes everything as this is what its being told.. i am thinking that the RPC needs to identify the ID of the photon object and then only change thats weapon.. well at least that is what I am trying to get workin right now BUT i dont know how to get the Senders Photon ID from the RPC!!
I think as soon as i work this out i will have this working as intended.. if anyone can help then that would be great!
Hi, Do you find solution, because I have exactly the same problem.
Answer by Promonster · Jul 29, 2016 at 12:32 PM
Can I get your code;WeaponManager? Now I'm studying this part.. please
Answer by juraj123 · Apr 15, 2018 at 12:17 PM
Please, how to do it, if Im using numbers - if I press 2, my pistol will setActive(true) and all will see it? Please help