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

How to sync animation without Animator ?

Discussion in 'Multiplayer' started by Paradoks, Dec 1, 2015.

  1. Paradoks

    Paradoks

    Joined:
    Oct 13, 2009
    Posts:
    436
    Hi,

    I am having a very hard time trying to sync an animation over unet.
    On my unit (wich could be the player too as they share the same scripts) from now i used to send over the network the name of the current animation with "RPC.target.others", to make the remote instance play the same animation. but now because of the full auhtoritative i can't.

    now i got something ugly like that in pseudo code:

    Code (CSharp):
    1.  
    2. [ClientCallback]
    3. Update(){
    4.  
    5.      if ( key.A){ PlayAnimation(A)}
    6.      if ( key.B){ PlayAnimation(B)}
    7. }
    8.  
    9. PlayAnimation(string animation){
    10.  
    11.      play(animation)
    12.      CmdPlayAnimation(animation)
    13. }
    14.  
    15. [Command]
    16. CmdPlayAnimation(string animation){
    17.  
    18.      play(animation)
    19.      RpcPlayAnimation(animation)
    20. }
    21.  
    22. [ClientRpc]
    23. RpcPlayAnimation(string animation){
    24.  
    25.       play(animation)
    26. }
    27.  
    If i dont do all thoses back and forth things, all the instance are not impacted.
    I can't believe there is no other what to simply synchronize animations.

    Any idea please?
     
  2. Velo222

    Velo222

    Joined:
    Apr 29, 2012
    Posts:
    1,437
    I'm having the exact same problem. How do you manually sync animations without using the Network Animator?
     
  3. Paradoks

    Paradoks

    Joined:
    Oct 13, 2009
    Posts:
    436
    @Velo222
    i did exactly as i wrote:

    Code (CSharp):
    1.  
    2. [ClientRpc]
    3.     public void RpcPlayAnimation(string animationName){
    4.  
    5.         if ( hasAuthority || isServer){ return;}
    6.  
    7.         if ( isMakingDamage == true){
    8.          
    9.             thisAnimation.Play(animationName,PlayMode.StopAll);
    10.          
    11.         }else{
    12.          
    13.             thisAnimation.CrossFade(animationName);
    14.         }
    15.     }
    16.  
    17.     [Command]
    18.     public void CmdPlayAnimation(string animationName){
    19.  
    20.         if ( isDead == true){ return;}
    21.  
    22.         RpcPlayAnimation(animationName);
    23.  
    24.         if ( isMakingDamage == true){
    25.      
    26.             thisAnimation.Play(animationName,PlayMode.StopAll);
    27.          
    28.         }else{
    29.          
    30.             thisAnimation.CrossFade(animationName);
    31.         }
    32.     }
    33.  
    34.     public void MoveTo(){
    35.          
    36.         if (thisMoveType == MoveType.run){
    37.  
    38.             PlayAnimation("Run");
    39.  
    40.         }else{
    41.  
    42.             PlayAnimation("Walk");
    43.         }
    44.     }
    45.  
    but this is very ugly and redundant.
     
  4. Velo222

    Velo222

    Joined:
    Apr 29, 2012
    Posts:
    1,437
    Thanks Paradoks.

    Are you using Legacy Animations, or Mecanim? I am using mecanim, and I've been playing with using another command, perhaps it's something for you to consider. Right now I only have it working from remote client to server, but not vice versa yet.

    Here is one of the commands:
    Code (CSharp):
    1. [Command]
    2.     void CmdPlayRunAnimation()
    3.     {
    4.         NetworkServer.FindLocalObject(thisNetworkIdentity.netId).GetComponent<Animator>().SetBool("isPathing", true);
    5.     }
    This will set a parameter on the server-side instance of the unit/gameobject. I'm assuming you only need to do another [ClientRpc] call in the other direction?

    I'm pretty new to this, so just throwing out some ideas.
     
  5. LeopardX

    LeopardX

    Joined:
    May 31, 2015
    Posts:
    64
    Cant you use a syncvar with a hook to a function to at least set the animation thats playing to everyone ?
     
  6. moco2k

    moco2k

    Joined:
    Apr 29, 2015
    Posts:
    294
    I actually sync animations in this way and it works quite fine.
     
  7. Paradoks

    Paradoks

    Joined:
    Oct 13, 2009
    Posts:
    436
    it's what i did before, but my problem is that sometimes i got two time the same animation running in a row.