Photon RPC doesn't broadcast properly.

I’m trying to sync player’s animation through network by using Photon RPC, but it doesn’t work.

Player can have 2 weapons and using only one of them once. So when player switch the weapon, I’ll broadcast that which weapon have to switch like this:

public void SetWeapon(Weapon weapon) {
	photonView.RPC("RPCSetWeapon", PhotonTargets.Others, weapon);
}

[PunRPC]
void RPCSetWeapon(Weapon weapon) {
	// Switch Animation
	characterAnimator.SetBool(weapon.ToString(), true);
}

It’s FPS game so I don’t have to set animator in local player, so used PhotonTargets.Others.

Also sync firing and reloading animations has same logic:

public void PlayFireAnimation() {
	photonView.RPC("RPCPlayFireAnimation", PhotonTargets.Others);
}

[PunRPC]
void RPCPlayFireAnimation() {
	characterAnimator.SetTrigger("Firing");
}

public void PlayReloadAnimation() {
	photonView.RPC("RPCPlayReloadAnimation", PhotonTargets.Others);
}

[PunRPC]
void RPCPlayReloadAnimation() {
	characterAnimator.SetTrigger("Reloading");
}

But when I run this game, nothing happens and only I got is these warning messages:

animator is not playing an animatorcontroller

So I checked which object got RPC like this:

void RPCSetWeapon(Weapon weapon) {
	if(photonView.isMine) {
		print("IS LOCAL PLAYER, SKIP IT.");
		return;
	}
	else {
		print("IS NOT LOCAL PLAYER.");
	}

But it always printing “IS NOT LOCAL PLAYER.”. Seriously, I don’t know why this happens, am I misunderstanding use RPC or something?

Please let me know how do I fix this, thanks.

Hi,

when using PhotonTargets.Others, the RPC is only called on other clients, but not on the local client. This is why if (photonView.isMine) is always false when you print logs to the console. If you want to switch the weapon also on the local client (which I assume) you have to use PhotonTargets.All instead of PhotonTargets.Others. For more information about RPCs you can take a look at the RPCs and RaiseEvent documentation page.

Another thing: you are using a ‘Weapon’ object as parameter for the RPC. Did you register a Custom Type for this object? If not, you have to do this. You can have a look at this documentation page to see how the concept works. If you however just need the name of the weapon, it’s enough to just pass weapon.ToString() as a parameter.