Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Multiplayer animation synchronization

Discussion in 'Multiplayer' started by ApiShiro, Apr 17, 2020.

  1. ApiShiro

    ApiShiro

    Joined:
    Apr 13, 2020
    Posts:
    5
    Hello, I actually have a problem, in my project unity when someone come in the game he instantiate a new GameObject which is the personage and he is able to move freely, but we always see other players repeat the animation "run" ( the first player see the second and the 3th run, the 2nd see the 1st et the 3th run etc... ) and when we stop to run with our personage the animation "run" stop. Here is some screens ( More precision, we don't know why but our boolean "run" aren't synchronised, for example, given player 1 and player 2, even when the boolean "Run" of player 2 is false, player 1 will see it "true" which means that he will see the other player run perpetually and we don't know how to fix it). Thanks for help :)

     
  2. voncarp

    voncarp

    Joined:
    Jan 3, 2012
    Posts:
    187
    I suspect you are using Photon since I see photonView.isMine.

    If you are looking to sync a bool they way you are doing it, something like this will achieve that:

    Code (CSharp):
    1.  public bool AnimationBool;
    2.  
    3.     Animator myAnimator;
    4.  
    5.     private void Start()
    6.     {
    7.         myAnimator = GetComponent<Animator>();
    8.     }
    9.  
    10.     private void Update()
    11.     {
    12.         if (photonView.isMine)
    13.         {
    14.             if (Input.GetKeyDown(KeyCode.Space))
    15.             {
    16.                 AnimationBool = !AnimationBool;
    17.             }
    18.         }
    19.         else
    20.         {
    21.             myAnimator.SetBool("Attack", AnimationBool);
    22.         }
    23.     }
    24.  
    25.     void OnPhotonSerializeView(PhotonStream stream, PhotonMessageInfo info)
    26.     {
    27.         if (stream.isWriting)
    28.         {
    29.             stream.SendNext(AnimationBool);
    30.         }
    31.         else
    32.         {
    33.             AnimationBool = (bool)stream.ReceiveNext();
    34.         }
    35.     }
    A few things have to happen for any of this to work in Photon. You have to be connected to the master server and be in a room. Another is the script that this is called on needs to be placed on the observed component of the photon view.
     
  3. Wirmroost

    Wirmroost

    Joined:
    Feb 5, 2020
    Posts:
    2
    How can you add a script in the observed componants of the player?
    (the players are already connected on the same room)
     
    Last edited: Apr 18, 2020
  4. voncarp

    voncarp

    Joined:
    Jan 3, 2012
    Posts:
    187
    You add it here

    download.png
     
  5. Wirmroost

    Wirmroost

    Joined:
    Feb 5, 2020
    Posts:
    2
    I tried but it doesnt work. Just for precision, our player is instantiated so it doesnt appear in the upper left hierarchy, only in the bottom one. Plus, it seems that i can only add prefabs inside the photonplayer.prefab folder