Author Topic: Help with syncing animations across network (using animation NOT animator)  (Read 4206 times)

acronyte

  • Newbie
  • *
  • Thank You
  • -Given: 9
  • -Receive: 0
  • Posts: 46
    • View Profile
Hi,
I'm creating a multiplayer game but have trouble syncing animations of my player prefab across network.

Everything is working fine EXCEPT for the actual animation of the player. Im using the 'TNautosync' which has the player prefab of each connected player syncing correctly....but only the position and rotation syncs NOT the animation.

I have attached a screenshot of my player prefab components...

Can someone please point me in the right direction to getting the prefab of each connected player sync across the network?

I'm sure its pretty simple for some of your experts, but I'm a beginner ,,,and would really appreciate a hand with it...

« Last Edit: September 28, 2015, 09:34:08 PM by acronyte »

acronyte

  • Newbie
  • *
  • Thank You
  • -Given: 9
  • -Receive: 0
  • Posts: 46
    • View Profile
Re: Help with syncing animations across network (using animation NOT animator)
« Reply #1 on: September 28, 2015, 06:46:36 PM »
BTW. Here is my character controller script.... just for reference..

  1. using UnityEngine;
  2. using System.Collections;
  3. using TNet;
  4.  
  5. [RequireComponent(typeof(CharacterController))]
  6. [RequireComponent(typeof(SimpleRpgAnimator))]
  7. public class SimpleRpgPlayerController : TNBehaviour
  8. {
  9.        
  10.         public bool clickToMove = false;
  11.         public List<string> clickableTags = new List<string>();
  12.         public bool keyboardControl = true;
  13.         public float walkSpeed = 3;
  14.         public float runSpeed = 6;
  15.         public float backpedalSpeed = 3;
  16.         public float turnSpeed = 6;
  17.         public float jumpPower = 8;
  18.         public float gravity = 20;
  19.         public float slopeLimit = 55;
  20.         public float fallThreshold = 10;
  21.         public float antiBunny = 0.75f;
  22.  
  23.         private bool _controllable = true;
  24.         private bool _running = true;
  25.         private bool _grounded = false;
  26.         private float _speed = 0;
  27.         private bool _autorun = false;
  28.         private Vector3 _velocity = Vector3.zero;
  29.         private float _fall_start = 0;
  30.         private float _input_x = 0;
  31.         private float _input_y = 0;
  32.         private float _input_s = 0;
  33.         private float _rotation = 0;
  34.         private Vector3 _last_position = Vector3.zero;
  35.         private float _animation_speed = 1;
  36.         private float _move_speed = 0;
  37.         private Vector3 _wanted_position = Vector3.zero;
  38.         private Vector3 _last_wanted_position = Vector3.zero;
  39.         private float _last_distance = 0;
  40.  
  41.         private Transform _t;
  42.         private CharacterController _controller;
  43.         private SimpleRpgAnimator _animator;
  44.  
  45.         public bool Grounded
  46.         {
  47.                 get { return _grounded; }
  48.                 set { _grounded = value; }
  49.         }
  50.  
  51.         public Vector3 Velocity
  52.         {
  53.                 get { return _velocity; }
  54.                 set { _velocity = value; }
  55.         }
  56.  
  57.         public float InputX
  58.         {
  59.                 get { return _input_x; }
  60.         }
  61.  
  62.         public float InputY
  63.         {
  64.                 get { return _input_y; }
  65.         }
  66.  
  67.         public float InputS
  68.         {
  69.                 get { return _input_s; }
  70.         }
  71.  
  72.         public float Rotation
  73.         {
  74.                 get { return _rotation; }
  75.         }
  76.  
  77.         public float FallPosition
  78.         {
  79.                 get { return _fall_start; }
  80.                 set { _fall_start = value; }
  81.         }
  82.  
  83.         public bool Controllable
  84.         {
  85.                 get { return _controllable; }
  86.                 set { _controllable = value; }
  87.         }
  88.  
  89.         [RFC]
  90.         void Start()
  91.         {
  92.  
  93.                 if(tno.isMine) {
  94.                 _t = transform;
  95.                 _controller = GetComponent<CharacterController>();
  96.                 _animator = GetComponent<SimpleRpgAnimator>();
  97.  
  98.                 _controller.slopeLimit = slopeLimit;
  99.                 _wanted_position = _t.position;
  100.         }
  101.         }
  102.  
  103.         void Update()
  104.         {
  105.                 if(tno.isMine) {
  106.                 /*
  107.                  * Uncomment this code if you want to make a toggle button for running / walking
  108.                 if(Input.GetButtonDown("ToggleRun"))
  109.                 {
  110.                         _running = !_running;
  111.                 }
  112.                 */
  113.                
  114.                 // If user middle-clicks, toggle autorun on/off
  115.                 if(Input.GetMouseButtonDown(2))
  116.                 {
  117.                         _autorun = !_autorun;
  118.                 }
  119.                
  120.                 // If on the ground, allow jumping
  121.                 if(_grounded)
  122.                 {
  123.                         if(_controllable)
  124.                         {
  125.                                 if(Input.GetButtonDown("Jump"))
  126.                                 {
  127.                                         _velocity.y = jumpPower;
  128.                                         _grounded = false;
  129.                                 }
  130.                         }
  131.                 }
  132.  
  133.                 // Toggle running / walking
  134.                 if(Input.GetKeyDown(KeyCode.R))
  135.                 {
  136.                         _running = !_running;
  137.                 }
  138.  
  139.                 if(_controllable)
  140.                 {
  141.                         if(clickToMove)
  142.                         {
  143.                                 if(Input.GetMouseButton(0))
  144.                                 {
  145.                                         Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
  146.                                         RaycastHit[] hits = Physics.RaycastAll(ray);
  147.  
  148.                                         foreach(RaycastHit hit in hits)
  149.                                         {
  150.                                                 bool done = false;
  151.  
  152.                                                 foreach(string tag in clickableTags)
  153.                                                 {
  154.                                                         if(hit.transform.CompareTag(tag))
  155.                                                         {
  156.                                                                 _wanted_position = hit.point;
  157.                                                                 _last_distance = Vector3.Distance(_t.position, _wanted_position);
  158.                                                                 done = true;
  159.                                                                 break;
  160.                                                         }
  161.                                                 }
  162.  
  163.                                                 if(done)
  164.                                                 {
  165.                                                         break;
  166.                                                 }
  167.                                         }
  168.                                 }
  169.                         }
  170.                 }
  171.                
  172.                 _animator.SetSpeed(_animation_speed);
  173.         }
  174.         }
  175.        
  176.         // Physics should be handled within FixedUpdate()
  177.         void FixedUpdate()
  178.         {
  179.                 if(tno.isMine) {
  180.                 _animation_speed = 1;
  181.                 float input_modifier = (_input_x != 0.0f && _input_y != 0.0f) ? 0.7071f : 1.0f;
  182.  
  183.                 if(_controllable)
  184.                 {
  185.                         if(keyboardControl)
  186.                         {
  187.                                 _input_x = Input.GetAxis("Horizontal");
  188.                                 _input_y = Input.GetAxis("Vertical");
  189.                                 _input_s = Input.GetAxis("Strafe");
  190.                                
  191.                         }
  192.                 }
  193.                
  194.                 // If autorun is enabled, set Y input to always be 1 until user uses the Y axis
  195.                 if(_autorun)
  196.                 {
  197.                         if(_input_y == 0)
  198.                         {
  199.                                 _input_y = 1;
  200.                         }
  201.                         else
  202.                         {
  203.                                 _autorun = false;
  204.                         }
  205.                 }
  206.                
  207.                 // If user is using strafe keys, override X axis
  208.                 if(_input_s != 0)
  209.                 {
  210.                         _input_x = _input_s;
  211.                 }
  212.                
  213.                 // If the user is not holding right-mouse button, rotate the player with the X axis instead of strafing
  214.                 if(!Input.GetMouseButton(1) &&
  215.                 _input_x != 0 &&
  216.                 _input_s == 0)
  217.                 {
  218.                         _t.Rotate(new Vector3(0, _input_x * (turnSpeed / 2.0f), 0));
  219.                         _rotation = _input_x;
  220.                         _input_x = 0;
  221.                 }
  222.                 else
  223.                 {
  224.                         _rotation = 0;
  225.                 }
  226.                
  227.                 // Movement direction and speed
  228.                 if(_input_y < 0)
  229.                 {
  230.                         _speed = backpedalSpeed;
  231.                 }
  232.                 else
  233.                 {
  234.                         if(_running)
  235.                         {
  236.                                 _speed = runSpeed;
  237.                         }
  238.                         else
  239.                         {
  240.                                 _speed = walkSpeed;
  241.                         }
  242.                 }
  243.  
  244.                 if(clickToMove)
  245.                 {
  246.                         if(_last_wanted_position != _wanted_position)
  247.                         {
  248.                                 float d = Vector3.Distance(_t.position, _wanted_position);
  249.  
  250.                                 if(d > _last_distance)
  251.                                 {
  252.                                         d = 0;
  253.                                 }
  254.                                 else
  255.                                 {
  256.                                         _last_distance = d;
  257.                                 }
  258.  
  259.                                 if(d >= 0.1f)
  260.                                 {
  261.                                         _t.LookAt(new Vector3(_wanted_position.x, _t.position.y, _wanted_position.z));
  262.                                         _input_y = Mathf.Clamp(d / 2f, 0, 1);
  263.                                 }
  264.                                 else
  265.                                 {
  266.                                         _last_wanted_position = _wanted_position;
  267.                                         _input_y = 0;
  268.                                 }
  269.                         }
  270.                 }
  271.                
  272.  
  273.                 // If on the ground, test to see if still on the ground and apply movement direction
  274.                 if(_grounded)
  275.                 {
  276.                         _velocity = new Vector3(_input_x * input_modifier, -antiBunny, _input_y * input_modifier);
  277.                         _velocity = _t.TransformDirection(_velocity) * _speed;
  278.  
  279.                         // Animation
  280.                         _move_speed = (_t.position - _last_position).magnitude;
  281.                         _last_position = _t.position;
  282.  
  283.                         if(_move_speed > 0)
  284.                         {
  285.                                 if(_move_speed > 0.07f)
  286.                                 {
  287.                                         if(Input.GetMouseButton(1) &&
  288.                                         _input_x != 0)
  289.                                         {
  290.                                                 if(_input_x < 0)
  291.                                                 {
  292.                                                         if(_input_y > 0)
  293.                                                         {
  294.                                                                 _animator.Action = "run_strafe_left_45";
  295.                                                         }
  296.                                                         else
  297.                                                         {
  298.                                                                 _animator.Action = "run_strafe_left_90";
  299.                                                         }
  300.                                                 }
  301.                                                 else
  302.                                                 {
  303.                                                         if(_input_y > 0)
  304.                                                         {
  305.                                                                 _animator.Action = "run_strafe_right_45";
  306.                                                         }
  307.                                                         else
  308.                                                         {
  309.                                                                 _animator.Action = "run_strafe_right_90";
  310.                                                         }
  311.                                                 }
  312.                                         }
  313.                                         else
  314.                                         {
  315.                                                 _animator.Action = "run";
  316.                                         }
  317.  
  318.                                         _animation_speed = 1;
  319.                                 }
  320.                                 else
  321.                                 {
  322.                                         if(Input.GetMouseButton(1) &&
  323.                                         _input_x != 0)
  324.                                         {
  325.                                                 if(_input_x < 0)
  326.                                                 {
  327.                                                         if(_input_y > 0)
  328.                                                         {
  329.                                                                 _animator.Action = "walk_strafe_left_45";
  330.                                                         }
  331.                                                         else if(_input_y < 0)
  332.                                                         {
  333.                                                                 _animator.Action = "walk_strafe_right_45";
  334.                                                         }
  335.                                                         else
  336.                                                         {
  337.                                                                 _animator.Action = "walk_strafe_left_90";
  338.                                                         }
  339.                                                 }
  340.                                                 else
  341.                                                 {
  342.                                                         if(_input_y > 0)
  343.                                                         {
  344.                                                                 _animator.Action = "walk_strafe_right_45";
  345.                                                         }
  346.                                                         else if(_input_y < 0)
  347.                                                         {
  348.                                                                 _animator.Action = "walk_strafe_left_45";
  349.                                                         }
  350.                                                         else
  351.                                                         {
  352.                                                                 _animator.Action = "walk_strafe_right_90";
  353.                                                         }
  354.                                                 }
  355.                                         }
  356.                                         else
  357.                                         {
  358.                                                 _animator.Action = "walk";
  359.                                         }
  360.  
  361.                                         _animation_speed = _move_speed * 13 + 1;
  362.  
  363.                                         if(_input_y < 0)
  364.                                         {
  365.                                                 _animation_speed = -_animation_speed;
  366.                                         }
  367.                                 }
  368.                         }
  369.                         else
  370.                         {
  371.                                 if(_rotation < 0)
  372.                                 {
  373.                                         _animator.Action = "shimmy_left";
  374.                                 }
  375.                                 else if(_rotation > 0)
  376.                                 {
  377.                                         _animator.Action = "shimmy_right";
  378.                                 }
  379.                                 else
  380.                                 {
  381.                                         _animator.Action = "idle";
  382.                                 }
  383.                         }
  384.  
  385.                         if(!Physics.Raycast(_t.position, -Vector3.up, 0.2f))
  386.                         {
  387.                                 _grounded = false;
  388.                         }
  389.                 }
  390.                 else
  391.                 {
  392.                         if(_velocity.y > 0)
  393.                         {
  394.                                 _animator.Action = "jump";
  395.                         }
  396.                         else
  397.                         {
  398.                                 _animator.Action = "fall";
  399.                         }
  400.  
  401.                         // Sets the falling start position to the highest point the player reaches
  402.                         if(_fall_start < _t.position.y)
  403.                         {
  404.                                 _fall_start = _t.position.y;
  405.                         }
  406.                 }
  407.  
  408.                 _velocity.y -= gravity * Time.deltaTime;
  409.                 _controller.Move(_velocity * Time.deltaTime);
  410.         }
  411. }
  412.        
  413.         void OnControllerColliderHit(ControllerColliderHit col)
  414.         {
  415.                 // This keeps the player from sticking to walls
  416.                 float angle = col.normal.y * 90;
  417.  
  418.                 if(angle < slopeLimit)
  419.                 {
  420.                         if(_grounded)
  421.                         {
  422.                                 _velocity = Vector3.zero;
  423.                         }
  424.  
  425.                         if(_velocity.y > 0)
  426.                         {
  427.                                 _velocity.y = 0;
  428.                         }
  429.                         else
  430.                         {
  431.                                 _velocity += new Vector3(col.normal.x, 0, col.normal.z).normalized;
  432.                         }
  433.  
  434.                         _grounded = false;
  435.                 }
  436.                 else
  437.                 {
  438.                         // Player is grounded here
  439.                         // If player falls too far, trigger falling damage
  440.                         if(_t.position.y < _fall_start - fallThreshold)
  441.                         {
  442.                                 FallingDamage(_fall_start - fallThreshold - _t.position.y);
  443.                         }
  444.                        
  445.                         _fall_start = _t.position.y;
  446.  
  447.                         _grounded = true;
  448.                         _velocity.y = 0;
  449.                 }
  450.         }
  451.        
  452.         public void FallingDamage(float fall_distance)
  453.         {
  454.                 Debug.Log("Fell " + fall_distance + " units.");
  455.         }
  456. }
  457.  

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: Help with syncing animations across network (using animation NOT animator)
« Reply #2 on: September 29, 2015, 04:48:08 PM »
TNet isn't able to send animation clips across the network. It's not a type it supports, so the sync won't work.

My advice is don't sync the animation. It's a silly thing to do. Visualization should be separate from networking. All visualization logic should occur as a result of movement and other factors. If I were you, I wouldn't sync the transforms either. Sync the player axes that cause the movement, and then periodically sync the position/rotation (every few seconds). Not only you will minimize the bandwidth required, but you will also improve the smoothness of your networked objects.

So in summary: sync the movement axes that cause the player to move (horizontal, vertical for example), and have each client do their own move logic based on the sync'd axes. Animations should be client-side, based on the movement: for example if the player is moving forward, play its walk/run animation.

acronyte

  • Newbie
  • *
  • Thank You
  • -Given: 9
  • -Receive: 0
  • Posts: 46
    • View Profile
Re: Help with syncing animations across network (using animation NOT animator)
« Reply #3 on: September 30, 2015, 03:30:56 PM »
Thanks so much for the help!...

I'll be using the TNautosync script to sync the axes....

I just have one more question.

In my controller script I posted has the axes as 'private' , so they dont show up inside the TNautoscript (in the inspector)

  1.     private float _input_x = 0;
  2.     private float _input_y = 0;
  3.     private float _input_s = 0;
  4.  

Is it safe to change them to 'public'? in order for the to be assigned in the inspector?

thanks again

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Sure, although I recommend just using a single Vector3 here.