I’m currently developing a multiplayer FPS, and I faced a problem right now.
I’m making a weapon switching system for my FPS game, but I have no idea how to do it.
The system I’m trying to make is pretty simple.
Whenever I press a specified button, it should disable component, and enable other component. (It needs to be component not a gameobject)
I used [PunRPC], tag but then I realised that it applies to every player which means everyplayer’s gun will be enabled.
So I’m asking to the internet on how to modify the component globally only for one player.
Hi,
RPC is one option you have. Another is to use OnPhotonSerializeView(…) where you can synchronize boolean values in order to describe which component is currently active. This can look like this:
public void OnPhotonSerializeView(PhotonStream stream, PhotonMessageInfo info)
{
if (stream.isWriting)
{
stream.SendNext(isComponentAActive);
stream.SendNext(isComponentBActive);
}
if (stream.isReading)
{
isComponentAActive = (bool) stream.ReceiveNext();
isComponentBActive = (bool) stream.ReceiveNext();
componentA.SetActive(isComponentAActive);
}
}
Make sure that, when you check your input, you only check the input of the local player to avoid changing the weapon for any other player. This should look like this:
public void Update()
{
if (!photonView.isMine)
{
return;
}
// Check input
}