How do I get photon to broadcast a signal to trigger an event?
EDIT: I found a solution using RPCs
I'm building a little multiplayer game. There's an event that, when triggered by one player, I would like all other players to see as well. Specifically, I would like an object to play an animation when a player presses an action button near it. However, I'm hitting this problem:
Player 1 starts the game, creates a room and joins it. Then, Player 2 starts the game and joins the room. Both players approach the object. When Player 1 presses their action button, the animation plays for both players. However, when Player 2 presses the action button, only they see the animation play.
My guess is that when Player 2 joins, their stream is prepared to receive the necessary data from Player 1, but Player 1's stream is not set up to broadcast to Player 2. How can I fix this? I thought about calling PhotonNetwork.Instantiate, but I don't want to create a new object for all players every time a player joins. Maybe I could delete objects if !PhotonView.IsMine, but I figure there must be a better way.
I've attached the code for reference:
using Photon.Pun;
using UnityEngine;
public class FountainController : MonoBehaviour, IPunObservable {
// flag that allows player to interact with the fountain
bool playerInTrigger;
// receives signal to play the sparkle animation
bool receivedSparkle;
// sends a signal to play the sparkle animation
bool sendSparkle;
Animator animator;
void Start () {
// initialize variables
playerInTrigger = false;
receivedSparkle = false;
sendSparkle = false;
animator = this.GetComponent<Animator> ();
}
void Update () {
if (receivedSparkle) {
// play animation if received a broadcasted signal
print ("received a sparkle");
animator.SetBool ("PlayAnimation", true);
receivedSparkle = false;
} else if (playerInTrigger && Input.GetKeyDown ("right shift")) {
// play animation if triggered by player
animator.SetBool ("PlayAnimation", true);
// broadcast the signal to trigger other players' animations
sendSparkle = true;
}
}
// used in Animation to end the clip
void TurnOffAnimation () {
animator.SetBool ("PlayAnimation", false);
}
// allow the event to happen if player's character enters trigger
void OnTriggerEnter2D (Collider2D other) {
if (!other.gameObject.GetComponent<PlayerController> ()) {
return;
}
PhotonView pv = other.GetComponent<PhotonView> ();
if (!pv.IsMine && PhotonNetwork.IsConnected) {
return;
}
playerInTrigger = true;
}
// prevent the event from happening if player's character leaves trigger
void OnTriggerExit2D (Collider2D other) {
if (!other.gameObject.GetComponent<PlayerController> ()) {
return;
}
PhotonView pv = other.GetComponent<PhotonView> ();
if (!pv.IsMine && PhotonNetwork.IsConnected) {
return;
}
playerInTrigger = false;
}
public void OnPhotonSerializeView (PhotonStream stream, PhotonMessageInfo messageInfo) {
if (stream.IsWriting) {
stream.SendNext (sendSparkle);
if (sendSparkle) {
// don't send multiple signals to play animation
sendSparkle = false;
}
} else {
receivedSparkle = (bool) stream.ReceiveNext ();
}
}
}
I figured it out, I needed to use RPCs
https://doc.photonengine.com/en-us/pun/v2/gameplay/rpcsandraiseevent