Search Unity

network animator not synching animations

Discussion in 'Multiplayer' started by Resilo, May 18, 2017.

  1. Resilo

    Resilo

    Joined:
    Dec 8, 2016
    Posts:
    139
    So my issue is that even with a network animator and all of the boxes checked the animations are still not seen by other players. my script for attacking works as follows. So im wondering how can i edit my script to properly sync the animations?

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.Networking;
    5.  
    6. public class clickattack : NetworkBehaviour  {
    7.     public GameObject targeting;
    8.     public GameObject sword;
    9.     public float strength;
    10.     public Animator anim;
    11.     public bool canclick;
    12.  
    13.     IEnumerator attackisopen()
    14.     {
    15.         canclick = true;
    16.         GetComponent<Attackdirection>().canattack = true;
    17.         yield return new WaitForSeconds(10 * Time.deltaTime);
    18.         sword.GetComponent<Gettindamaged>().candamage = true;
    19.         yield return new WaitForSeconds(80 * Time.deltaTime);
    20.         GetComponent<Attackdirection>().canattack = false;
    21.         canclick = false;
    22.     }
    23.  
    24.     void attacking()
    25.     {
    26.         if(Input.GetKeyDown(KeyCode.F) && canclick == false)
    27.         {
    28.  
    29.             StartCoroutine(attackisopen());
    30.             attackanimationsuccesful();
    31.         }
    32.     }
    33.  
    34.  
    35.  
    36.     void attackanimationsuccesful()
    37.     {
    38.         if (GetComponent<Attackdirection>().attackingleft == true)
    39.         {
    40.             anim.SetTrigger("Attackleft");
    41.         }
    42.         if (GetComponent<Attackdirection>().attackingup == true )
    43.         {
    44.             anim.SetTrigger("Attackup");
    45.         }
    46.         if (GetComponent<Attackdirection>().attackingright == true)
    47.         {
    48.             anim.SetTrigger("Attackright");
    49.         }
    50.         if (GetComponent<Attackdirection>().attackingdown == true)
    51.         {
    52.             anim.SetTrigger("Attackdown");
    53.         }
    54.     }
    55.  
    56.  
    57.     // Use this for initialization
    58.     void Start () {
    59.     }
    60.  
    61.  
    62.  
    63.     // Update is called once per frame
    64.     void Update () {
    65.         if (!isLocalPlayer)
    66.         {
    67.             return;
    68.         }
    69.         attacking();
    70.  
    71.     }
    72. }
    73.  
     
  2. TwoTen

    TwoTen

    Joined:
    May 25, 2016
    Posts:
    1,168
    Not using NetworkAnimator: (Note, I have Not compiled the code. Just wrote it in Notepad. Excuse any errors)
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.Networking;
    5. public class clickattack : NetworkBehaviour  {
    6.     public GameObject targeting;
    7.     public GameObject sword;
    8.     public float strength;
    9.     public Animator anim;
    10.     public bool canclick;
    11.     IEnumerator attackisopen()
    12.     {
    13.         canclick = true;
    14.         GetComponent<Attackdirection>().canattack = true;
    15.         yield return new WaitForSeconds(10 * Time.deltaTime);
    16.         sword.GetComponent<Gettindamaged>().candamage = true;
    17.         yield return new WaitForSeconds(80 * Time.deltaTime);
    18.         GetComponent<Attackdirection>().canattack = false;
    19.         canclick = false;
    20.     }
    21.     void attacking()
    22.     {
    23.         if(Input.GetKeyDown(KeyCode.F) && canclick == false)
    24.         {
    25.             StartCoroutine(attackisopen());
    26.             attackanimationsuccesful();
    27.         }
    28.     }
    29.  
    30.     [Command]
    31.     void Cmd_Animate(string trigger)
    32.     {
    33.         Rpc_Animate(trigger);
    34.     }
    35.  
    36.     [ClientRpc]
    37.     void Rpc_Animate(string trigger)
    38.     {
    39.         anim.SetTrigger(trigger);
    40.     }
    41.     void attackanimationsuccesful()
    42.     {
    43.         if(!isLocalPlayer)
    44.             return;
    45.      
    46.         if (GetComponent<Attackdirection>().attackingleft == true)
    47.         {
    48.             anim.SetTrigger("Attackleft");
    49.             Cmd_Animate("Attackleft");
    50.         }
    51.         if (GetComponent<Attackdirection>().attackingup == true )
    52.         {
    53.             anim.SetTrigger("Attackup");
    54.             Cmd_Animate("Attackup");
    55.         }
    56.         if (GetComponent<Attackdirection>().attackingright == true)
    57.         {
    58.             anim.SetTrigger("Attackright");
    59.             Cmd_Animate("Attackright");
    60.         }
    61.         if (GetComponent<Attackdirection>().attackingdown == true)
    62.         {
    63.             anim.SetTrigger("Attackdown");
    64.             Cmd_Animate("Attackright");
    65.         }
    66.     }
    67.     // Use this for initialization
    68.     void Start () {
    69.     }
    70.     // Update is called once per frame
    71.     void Update () {
    72.         if (!isLocalPlayer)
    73.         {
    74.             return;
    75.         }
    76.         attacking();
    77.     }
    78. }
    79.  
    Any questions regarding the code, let me know
     
  3. Resilo

    Resilo

    Joined:
    Dec 8, 2016
    Posts:
    139
    just to verify the [Command] synchronizes it to the server and [ClientRpc] synchronizes it to all the other connect clients correct?
     
  4. TwoTen

    TwoTen

    Joined:
    May 25, 2016
    Posts:
    1,168
    Mark a method with Command and when you call it. A packet is sent to the server instructing it to run the method. So basically what's in the Command method is server code. Rpc is same but flipped.
    So sent from server to clients to run said code.
     
  5. Resilo

    Resilo

    Joined:
    Dec 8, 2016
    Posts:
    139
    script works perfect just has to remove the inital anim.("attack[direction]")
     
    Last edited: May 19, 2017
  6. TwoTen

    TwoTen

    Joined:
    May 25, 2016
    Posts:
    1,168
    I would not do that. That will cause latency for the local client doing the animation. I would do this:
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.Networking;
    5. public class clickattack : NetworkBehaviour  {
    6.     public GameObject targeting;
    7.     public GameObject sword;
    8.     public float strength;
    9.     public Animator anim;
    10.     public bool canclick;
    11.     IEnumerator attackisopen()
    12.     {
    13.         canclick = true;
    14.         GetComponent<Attackdirection>().canattack = true;
    15.         yield return new WaitForSeconds(10 * Time.deltaTime);
    16.         sword.GetComponent<Gettindamaged>().candamage = true;
    17.         yield return new WaitForSeconds(80 * Time.deltaTime);
    18.         GetComponent<Attackdirection>().canattack = false;
    19.         canclick = false;
    20.     }
    21.     void attacking()
    22.     {
    23.         if(Input.GetKeyDown(KeyCode.F) && canclick == false)
    24.         {
    25.             StartCoroutine(attackisopen());
    26.             attackanimationsuccesful();
    27.         }
    28.     }
    29.     [Command]
    30.     void Cmd_Animate(string trigger)
    31.     {
    32.         Rpc_Animate(trigger);
    33.     }
    34.     [ClientRpc]
    35.     void Rpc_Animate(string trigger)
    36.     {
    37.         if(!isLocalPlayer)
    38.             anim.SetTrigger(trigger);
    39.     }
    40.     void attackanimationsuccesful()
    41.     {
    42.         if(!isLocalPlayer)
    43.             return;
    44.    
    45.         if (GetComponent<Attackdirection>().attackingleft == true)
    46.         {
    47.             anim.SetTrigger("Attackleft");
    48.             Cmd_Animate("Attackleft");
    49.         }
    50.         if (GetComponent<Attackdirection>().attackingup == true )
    51.         {
    52.             anim.SetTrigger("Attackup");
    53.             Cmd_Animate("Attackup");
    54.         }
    55.         if (GetComponent<Attackdirection>().attackingright == true)
    56.         {
    57.             anim.SetTrigger("Attackright");
    58.             Cmd_Animate("Attackright");
    59.         }
    60.         if (GetComponent<Attackdirection>().attackingdown == true)
    61.         {
    62.             anim.SetTrigger("Attackdown");
    63.             Cmd_Animate("Attackright");
    64.         }
    65.     }
    66.     // Use this for initialization
    67.     void Start () {
    68.     }
    69.     // Update is called once per frame
    70.     void Update () {
    71.         if (!isLocalPlayer)
    72.         {
    73.             return;
    74.         }
    75.         attacking();
    76.     }
    77. }
    78.  
     
  7. Resilo

    Resilo

    Joined:
    Dec 8, 2016
    Posts:
    139
    yes i have noticed that is an issue but with the original script it worked perfectly but the animation activated twice
     
  8. TwoTen

    TwoTen

    Joined:
    May 25, 2016
    Posts:
    1,168
    Yes, the updated one I posted doesn't play twice but has no latency.
     
  9. Resilo

    Resilo

    Joined:
    Dec 8, 2016
    Posts:
    139
    oh i see why now thanks for the edit. is there a way to further reduce the time between when the animation starts on your client and the other client. i noticed even edited theres still some issues with a delay between when the animations both started
     
    Last edited: May 19, 2017
  10. TwoTen

    TwoTen

    Joined:
    May 25, 2016
    Posts:
    1,168
    No, That's latency and can NEVER be resolved. Usually refered to as "ping" in video games.
     
  11. Resilo

    Resilo

    Joined:
    Dec 8, 2016
    Posts:
    139
    ah ok makes complete sense figured id ask as i know that unity deals with some more complex concepts such as the send rate and such but if its just regular ping then ill have to deal with it some other way