Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
  • Help Room /
avatar image
0
Question by james_kelleher · Apr 14, 2020 at 02:27 PM · networkingphoton

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 ();
         }
     }
 } 


Comment
Add comment · Show 1
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image james_kelleher · Apr 11, 2020 at 10:32 PM 0
Share

I figured it out, I needed to use RPCs

https://doc.photonengine.com/en-us/pun/v2/gameplay/rpcsandraiseevent

0 Replies

· Add your reply
  • Sort: 

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

249 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

How both players can control the soccer ball in 2 player football game 1 Answer

Destroy PhotonView component but not GameObject 0 Answers

Setting and serializing an object state visible/invisible through a random variable? 0 Answers

Photon Join Random CustomRoomProperties not working 0 Answers

I'm learning Photon Unity Network. Everything is okay but I get this pink screen error in the desktop version. Why ? 2 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges