Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Voting for the Unity Awards are OPEN! We’re looking to celebrate creators across games, industry, film, and many more categories. Cast your vote now for all categories
    Dismiss Notice
  3. Dismiss Notice

Photon multiplayer animations sync question

Discussion in 'Scripting' started by Jeepster, Aug 20, 2018.

  1. Jeepster

    Jeepster

    Joined:
    Jan 23, 2014
    Posts:
    401
    Hi,


    I've setup Photon in my project and I can have two players run around my scene but when the animation for one is activated it also shows on the other player.

    Here's the animation code:

    Code (CSharp):
    1.  public void OnPhotonSerializeView(PhotonStream stream, PhotonMessageInfo info)
    2.    
    3.  
    4.   if (stream.isWriting)
    5.         {
    6.             //This is our player, we need to send our actual position to network
    7.             stream.SendNext(transform.position);
    8.             stream.SendNext(transform.rotation);
    9.  
    10.  
    11.             //Here we stream our animations to the network
    12.             stream.SendNext(anim.GetBool("shootidle"));
    13.             stream.SendNext(anim.GetBool("shootforward"));
    14.             stream.SendNext(anim.GetBool("idle"));
    15.             stream.SendNext(anim.GetBool("forward"));
    16.             stream.SendNext(anim.GetBool("fwdLeft"));
    17.             stream.SendNext(anim.GetBool("fwdRight"));
    18.             stream.SendNext(anim.GetBool("left"));
    19.             stream.SendNext(anim.GetBool("right"));//FWDAnimation is just an example, use your named animations
    20.        
    21.         }
    22.         else
    23.         {
    24.            
    25.             realPosition = (Vector3)stream.ReceiveNext();
    26.             realRotation = (Quaternion)stream.ReceiveNext();
    27.  
    28.  
    29.             //Here we make sure other players receive the sent animations of my player, through the network.
    30.             anim.SetBool("shootidle", (bool)stream.ReceiveNext());
    31.             anim.SetBool("shootforward", (bool)stream.ReceiveNext());
    32.             anim.SetBool("idle", (bool)stream.ReceiveNext());
    33.             anim.SetBool("forward", (bool)stream.ReceiveNext());
    34.             anim.SetBool("fwdLeft", (bool)stream.ReceiveNext());
    35.             anim.SetBool("fwdRight", (bool)stream.ReceiveNext());
    36.             anim.SetBool("left", (bool)stream.ReceiveNext());
    37.             anim.SetBool("right", (bool)stream.ReceiveNext());
    38.         }

    Right now it's the same player that spawns, but in different instances and the controls are individual (not affecting both players)

    How come the animations show on both players when I move one, and how do I fix it?
     
  2. Doug_B

    Doug_B

    Joined:
    Jun 4, 2017
    Posts:
    1,596
    Is there any particular reason why you are re-coding all the serialization yourself rather than using the Photon supplied solutions?
     
  3. Jeepster

    Jeepster

    Joined:
    Jan 23, 2014
    Posts:
    401
    I've been following a tutorial to try and get a better understanding of how it all works, but the tutorial is rather old by now and it down't include the animations I put in the code above. I'm new to Photon, but is there a serialization script where I could add animations f.ex or what supplied solutions are you referring to?
     
  4. Doug_B

    Doug_B

    Joined:
    Jun 4, 2017
    Posts:
    1,596
    This tutorial series is quite good. However, it appears that they may be using the same serialisation method you are.

    So try this tutorial (around 3 minutes in) as well where he shows using the Photon animator and transform observers which are simpler to use if you are not doing anything unique or special.
     
    Jeepster likes this.
  5. Jeepster

    Jeepster

    Joined:
    Jan 23, 2014
    Posts:
    401
    Thanks for the tutorial. I found out why my instance plays the same animations as the master and it's because I left the animator component on instead of turning it off on the prefab, since I activate it through script so that it becomes independent for each instanced player :) very quick fix wuhuw :D
     
    Doug_B likes this.
  6. Doug_B

    Doug_B

    Joined:
    Jun 4, 2017
    Posts:
    1,596
    Nice. Glad you got it working. :)