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. Dismiss Notice

Networked animations?

Discussion in 'General Discussion' started by TylerPerry, Apr 23, 2014.

  1. TylerPerry

    TylerPerry

    Joined:
    May 29, 2011
    Posts:
    5,577
    I'm wondering how people have their animations play over networks? Is the normal thing to do to play a animation that is logical from their movement, or to actually send over the network which animation to play? Just out of curiosity.
     
  2. Tiny-Tree

    Tiny-Tree

    Joined:
    Dec 26, 2012
    Posts:
    1,314
    Im using two method for a fps, dont know yet which one to use,(1) i calculate that locally, take the velocity on X Y and Z instead of the input.getaxis(strafe, forward backward, falling) and i send RFC for Shoot/gethit/die/throw grenade states

    or (2)you have an int that you sync over network, its your anim states, each time it change you sync it.

    (1) is not very accurate but its save bandwhich (2) is accurate and anim play almost without delay at the cost of bandwhich
     
  3. goat

    goat

    Joined:
    Aug 24, 2009
    Posts:
    5,182
    Search for a guy named 'whippets' in the forums and PM him. If I remember he's doing animations and had to address a problem with root-motion messing up the animations due to some type of networking restraint.

    Off hand, I think you would send minimal information: the vectors with direction and the current animation. Better ask whippets because you get to the point of asking the question is the server (world) multitasking a large numbers of clients and they sync to the game play on the server (seems a must) or do you have some type of peer-to-peer situation where a server is elected (meaning a client could elect itself as server and run in standalone, networkless mode).
     
  4. Glader

    Glader

    Joined:
    Aug 19, 2013
    Posts:
    449
    Here is some code from I grabbed from my prototype for networked movement, the direction of movement is computed in another function and eventually passed to this function. I have some sort of blending stuff on Mecanim, honestly don't know the thing that well but it works, and here is the code. I think it's quite possible to deduce the animations based on strictly data sent from the other client. If I'm bending over to pick up a sword I only need to tell them that I picked up an item. I don't need to tell them what animation to play for it too.

    However, this relies on you making it so that your client-side animations are calculated based on the same data. Otherwise I can see why it'd be difficult to get animations to work well on another client for that player. I took that design route before and it ends badly with an ugly switch statement and flag enums lol.

    Test code from a prototype below:

    Code (csharp):
    1.  
    2. if (direction == Vector3.zero)
    3. {
    4.     animator.SetFloat("Speed", Mathf.Lerp(NetworkObjectDirection.z, 0, time));
    5.     animator.SetFloat("Strafe", Mathf.Lerp(NetworkObjectDirection.x, 0, time));
    6.     time += Time.deltaTime * 2;
    7.  
    8. }
    9. else
    10. {
    11.  
    12.     animator.SetFloat("Speed", direction.z);
    13.     animator.SetFloat("Strafe", direction.x);
    14.     time = 0;
    15.     NetworkObjectDirection = direction;
    16. }
    17.  
    Some of that code you don't really need like the lerp, I only did that so I could simulate the slow down in the animations. Also, likely not the most efficient way to calculate all this stuff. And that NetworkObjectDirection is in that class scope (this isn't in the same class where movement is handled) and is set so that when the object is still it knows from what position it needs to simulate the slow down from... I think? I wrote it one day like a month ago =/ so I don't really want to poke at it to figure out if that's exactly why.

    But that's not important. You can calculate animations without networking the animation information in the least.

    Edit: This solution is also dead-reckoning friendly too.

    Edit:Edit: It turns out I'm calling this every frame so I probably didn't need 2 Vector3. Probably a remnant of another trial and error attempt at it lol.
     
    Last edited: Apr 23, 2014