NEED HELP WITH WEAPON SWITCH SYNC
hi, i am still new to photon networking, and i am having trouble with weapon switch. i want the weapon switch to update to the other clients.
here's my code for weapon switch. i dont know where to start XD
public int selectWeapon = 0;
void Start()
{
SelectWeapon();
}
void Update()
{
SelectWeapon();
if (Input.GetAxis("Mouse ScrollWheel") > 0f)
{
if (selectWeapon >= transform.childCount - 1)
selectWeapon = 0;
else
selectWeapon++;
}
if (Input.GetAxis("Mouse ScrollWheel") < 0f)
{
if (selectWeapon <= 0)
selectWeapon = transform.childCount - 1;
else
selectWeapon--;
}
if (Input.GetKeyDown(KeyCode.Alpha1))
{
selectWeapon = 0;
}
if (Input.GetKeyDown(KeyCode.Alpha2))
{
selectWeapon = 1;
}
if (Input.GetKeyDown(KeyCode.Alpha3))
{
selectWeapon = 2;
}
}
public void SelectWeapon()
{
int i = 0;
foreach (Transform weapon in transform)
{
if (i == selectWeapon)
weapon.gameObject.SetActive(true);
else
weapon.gameObject.SetActive(false);
i++;
}
}
here's my code for weapon switch. i dont know where to start XD
public int selectWeapon = 0;
void Start()
{
SelectWeapon();
}
void Update()
{
SelectWeapon();
if (Input.GetAxis("Mouse ScrollWheel") > 0f)
{
if (selectWeapon >= transform.childCount - 1)
selectWeapon = 0;
else
selectWeapon++;
}
if (Input.GetAxis("Mouse ScrollWheel") < 0f)
{
if (selectWeapon <= 0)
selectWeapon = transform.childCount - 1;
else
selectWeapon--;
}
if (Input.GetKeyDown(KeyCode.Alpha1))
{
selectWeapon = 0;
}
if (Input.GetKeyDown(KeyCode.Alpha2))
{
selectWeapon = 1;
}
if (Input.GetKeyDown(KeyCode.Alpha3))
{
selectWeapon = 2;
}
}
public void SelectWeapon()
{
int i = 0;
foreach (Transform weapon in transform)
{
if (i == selectWeapon)
weapon.gameObject.SetActive(true);
else
weapon.gameObject.SetActive(false);
i++;
}
}
0
Comments
-
Hi @irvintiu,
for multiplayer in combination with PUN you should firstly add a reference to the game object's PhotonView component. Best way to do so is to have the code snippetprivate PhotonView photonView;
in your class. Then in the Awake() or Start() function you get the reference by usingphotonView = GetComponent<PhotonView>();
. Whenever you process input you should add a 'isMine' condition before the processing. In this case you can add the following at the beginning of the Update() function:if (!photonView.isMine) return;
. This prevents processing on game objects, that aren't owned by you (very important for two or more players).
Whenever the client now switches his weapon, you have to inform the other clients as well. This can be done by using RPCs. In this case you can usephotonView.RPC("SwitchWeapon", PhotonTargets.All, selectWeapon);
to call the RPC on each client. Do not call RPC in every update, just call it if the weapon actually has been switched. Each of the clients now need a receiving function, so you have to add[PunRPC] public void SwitchWeapon(int selectWeapon) { };
.
For further reading I would recommend you taking a look at the Basics Tutorial and the RPCs and RaiseEvent guide.0 -
thanks man. it helps0