Search Unity

Advice for syncing sprites over the network? 2D top down

Discussion in 'Multiplayer' started by Denchyaknow, Sep 19, 2015.

  1. Denchyaknow

    Denchyaknow

    Joined:
    Aug 4, 2015
    Posts:
    31
    So I have a script that grabs touch input and generates movement, then it feeds some variables it generated into a script for storage where other scripts can generate code based on those variables.

    So on the client side a player can swipe and the his player moves and animates his sprite by grabbing the directions and invoking a cycle of sprites, the movement syncs over the network but the sprites so not, I am a little confused on how to get the sprite renderer to sync, I tried syncing a sprite but that was a mess lol, so right now I amd trying to make a int syncvar work.

    if the player is moving then the sync var will be 0,1,2,3 if he is still then it will be 4, here is my sync animetion script and my player animation script, the player animation script seems to work fine but only the client can see his sprite animation. I was going to use a animator for this but it seemed reall redundant since i have alooot of sprite sheets. i fugured code would be the best.


    This is the player animation script that current works
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. using UnityEngine.Networking;
    4.  
    5. public class Player_Animation : NetworkBehaviour
    6. {
    7.     public V_Database V_Link;
    8.     public int anime_Direction; // 0=down 1=left 2=right 3=up
    9.     public SpriteRenderer current_Sprite; //0-2 = down 3-5 = left 6-8 = right 9-11 = up
    10.     public Rigidbody2D move_Body;
    11.     public float anime_Speed = 1;
    12.     public float anime_SpeedAlteration = 1;
    13.     public float move_Magnitude
    14.     {
    15.         get
    16.         {
    17.             float magnitude = 0;
    18.             magnitude = V_Link.move_Magnitude;
    19.             return magnitude;
    20.         }
    21.     }
    22.     public Vector2 move_Direction
    23.     {
    24.         get
    25.         {
    26.             Vector2 direction = new Vector2(0, 0);
    27.             direction = V_Link.touch_Left_Direction;
    28.             return direction;
    29.         }
    30.     }
    31.     public bool move_IsMoving
    32.     {
    33.         get
    34.         {
    35.             bool isMoving = false;
    36.             isMoving = V_Link.move_IsMoving;
    37.             return isMoving;
    38.         }
    39.     }
    40.     private float method_Time
    41.     {
    42.         get{return anime_Speed;}
    43.     }
    44.  
    45.     public Sprite[] anime_Frames;
    46.  
    47.     private bool anime_Trigger = false;
    48.     // Use this for initialization
    49.     void Start () {
    50.         V_Link = transform.root.GetComponent<V_Database>();
    51.  
    52.         current_Sprite = GetComponentInChildren<SpriteRenderer>();
    53.         move_Body = transform.root.GetComponent<Rigidbody2D>();
    54.         gameObject.name = netId.ToString();
    55.     }
    56.     void LateUpdate()
    57.     {
    58.         if(!isLocalPlayer)
    59.         {
    60.             return;
    61.         }
    62.  
    63.         Share();
    64.     }
    65.     void Share()
    66.     {
    67.      
    68.         V_Link.anime_Direction = anime_Direction;
    69.         V_Link.anime_Speed = anime_Speed;
    70.     }
    71.  
    72.     // Update is called once per frame
    73.     void Update () {
    74.  
    75.         if(!isLocalPlayer)
    76.         {
    77.             return;
    78.         }
    79.      
    80.         anime_Speed = move_Magnitude * anime_SpeedAlteration;
    81.         if(move_IsMoving == true && anime_Trigger == false)
    82.         {
    83.             anime_Trigger = true;
    84.             DirectionalUpdate(move_Direction);
    85.         }
    86.         else if(move_IsMoving == false && anime_Trigger == true)
    87.         {
    88.             Stop_MoveAnimation();
    89.         }
    90.     }
    91.    
    92.     public void Stop_MoveAnimation()
    93.     {
    94.         if (IsInvoking("AnimateLink"))
    95.         {
    96.             CancelInvoke("AnimateLink");
    97.             anime_Trigger = false;
    98.             anime_Direction = 4;
    99.         }
    100.     }
    101.     public void Play_MoveAnimation()
    102.     {
    103.         Stop_MoveAnimation();
    104.         InvokeRepeating("AnimateLink", 0, method_Time);
    105.              
    106.     }
    107.     private void AnimateLink()
    108.     {
    109.         StartCoroutine("Animate");
    110.     }
    111.     public IEnumerator Animate()
    112.     {
    113.         switch(anime_Direction)
    114.         {
    115.             case 0:
    116.                 yield return new WaitForSeconds(anime_Speed / 4);
    117.                 current_Sprite.sprite = anime_Frames[0];
    118.                 yield return new WaitForSeconds(anime_Speed / 4);
    119.                 current_Sprite.sprite = anime_Frames[1];
    120.                 yield return new WaitForSeconds(anime_Speed / 4);
    121.                 current_Sprite.sprite = anime_Frames[2];
    122.                 yield return new WaitForSeconds(anime_Speed / 4);
    123.                 current_Sprite.sprite = anime_Frames[1];
    124.                 yield return new WaitForSeconds(anime_Speed / 4);
    125.  
    126.                 break;
    127.  
    128.             case 1:
    129.                 yield return new WaitForSeconds(anime_Speed / 4);
    130.                 current_Sprite.sprite = anime_Frames[3];
    131.                 yield return new WaitForSeconds(anime_Speed / 4);
    132.                 current_Sprite.sprite = anime_Frames[4];
    133.                 yield return new WaitForSeconds(anime_Speed / 4);
    134.                 current_Sprite.sprite = anime_Frames[5];
    135.                 yield return new WaitForSeconds(anime_Speed / 4);
    136.                 current_Sprite.sprite = anime_Frames[4];
    137.                 yield return new WaitForSeconds(anime_Speed / 4);
    138.  
    139.                 break;
    140.             case 2:
    141.                 yield return new WaitForSeconds(anime_Speed / 4);
    142.                 current_Sprite.sprite = anime_Frames[6];
    143.                 yield return new WaitForSeconds(anime_Speed / 4);
    144.                 current_Sprite.sprite = anime_Frames[7];
    145.                 yield return new WaitForSeconds(anime_Speed / 4);
    146.                 current_Sprite.sprite = anime_Frames[8];
    147.                 yield return new WaitForSeconds(anime_Speed / 4);
    148.                 current_Sprite.sprite = anime_Frames[7];
    149.                 yield return new WaitForSeconds(anime_Speed / 4);
    150.  
    151.                 break;
    152.             case 3:
    153.                 yield return new WaitForSeconds(anime_Speed / 4);
    154.                 current_Sprite.sprite = anime_Frames[9];
    155.                 yield return new WaitForSeconds(anime_Speed / 4);
    156.                 current_Sprite.sprite = anime_Frames[10];
    157.                 yield return new WaitForSeconds(anime_Speed / 4);
    158.                 current_Sprite.sprite = anime_Frames[11];
    159.                 yield return new WaitForSeconds(anime_Speed / 4);
    160.                 current_Sprite.sprite = anime_Frames[10];
    161.                 yield return new WaitForSeconds(anime_Speed / 4);
    162.  
    163.                 break;
    164.         }
    165.     }
    166.     void DirectionalUpdate(Vector2 swipeLength)
    167.     {
    168.         //Vector2 swipeLength = (V_LINK.touch_Left_Pos_End - V_LINK.touch_Left_Pos_Start);
    169.         //Debug.Log(swipeLength);
    170.         if (swipeLength.y > 0) //up not down++++++++++++++++++++++++++++++++++++
    171.         {
    172.             if (swipeLength.x > 0) //right not left-----------------------------
    173.             {
    174.                 if (swipeLength.x > swipeLength.y)
    175.                 {
    176.                     anime_Direction = 2; //play right frames
    177.                 }
    178.                 else
    179.                 {
    180.                     anime_Direction = 3;  //play up frames
    181.                 }
    182.             }
    183.             else if (swipeLength.x < 0) //left not right------------------------
    184.             {
    185.                 if (swipeLength.x * -1 > swipeLength.y)
    186.                 {
    187.                     anime_Direction = 1;  //play left frames
    188.                 }
    189.                 else
    190.                 {
    191.                     anime_Direction = 3; //play up frames
    192.                 }
    193.             }
    194.             Play_MoveAnimation();
    195.         }
    196.         else if (swipeLength.y < 0) //down not up +++++++++++++++++++++++++++++++
    197.         {
    198.             if (swipeLength.x > 0) //right not left------------------------------
    199.             {
    200.                 if (swipeLength.x * -1 < swipeLength.y)
    201.                 {
    202.                     anime_Direction = 2; //play right frames
    203.                 }
    204.                 else //play down frames
    205.                 {
    206.                     anime_Direction = 0;  //play down frames
    207.                 }
    208.             }
    209.             else if (swipeLength.x < 0) //left not right-------------------------
    210.             {
    211.                 if (swipeLength.x < swipeLength.y)
    212.                 {
    213.                     anime_Direction = 1; //play left frames
    214.                 }
    215.                 else
    216.                 {
    217.                     anime_Direction = 0; //play down frames
    218.                 }
    219.             }
    220.             Play_MoveAnimation();
    221.         }
    222.         else
    223.         {
    224.             Stop_MoveAnimation();
    225.         }
    226.        
    227.     }
    228. }
    229.  


    And this is the problem script, at this point I screwed around with it so much I have no idea what I should do to fix it and its a bit of a mess sorry about that :)

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. using UnityEngine.Networking;
    4. public class Player_SyncAnimation : NetworkBehaviour {
    5.     public V_Database V_Link;
    6.     public SpriteRenderer current_Sprite;
    7.     public Rigidbody2D move_Body;
    8.     public Sprite[] anime_Frames;
    9.     public int anime_Direction;
    10.  
    11.     public float anime_Speed
    12.     {
    13.         get
    14.         {
    15.             float speed = 0;
    16.             speed = move_Body.velocity.magnitude * anime_SpeedAlteration;
    17.             return speed;
    18.         }
    19.     }
    20.     public float anime_SpeedAlteration;
    21.     [SyncVar]
    22.     public int syncDirection;
    23.  
    24.     public bool anime_Trigger;
    25.     // Use this for initialization
    26.     void Start () {
    27.         V_Link = transform.root.GetComponent<V_Database>();
    28.         current_Sprite = GetComponentInChildren<SpriteRenderer>();
    29.         anime_Frames = transform.root.GetComponent<Player_Animation>().anime_Frames;
    30.         move_Body = transform.root.GetComponent<Rigidbody2D>();
    31.     }
    32.    
    33.     // Update is called once per frame
    34.     void Update () {
    35.  
    36.  
    37.        
    38.  
    39.         if(isLocalPlayer)
    40.         {
    41.             anime_Direction = V_Link.anime_Direction;
    42.             TransmitDirectionChange();
    43.             return;
    44.         }
    45.         /*
    46.         if (syncDirection != 4 && anime_Trigger == false)
    47.         {
    48.             anime_Trigger = true;
    49.             CmdPlay_MoveAnimation();
    50.             Debug.Log(syncDirection);
    51.         }
    52.         else if(syncDirection == 4 && anime_Trigger == true)
    53.         {
    54.             CmdStop_MoveAnimation();
    55.             anime_Trigger = false;
    56.         }
    57.         if(!isLocalPlayer)
    58.         {
    59.             CmdPlay_MoveAnimation();
    60.         }
    61.  
    62.     */
    63.     }
    64.     [Command]
    65.     public void CmdStop_MoveAnimation()
    66.     {
    67.         if (IsInvoking("CmdAnimateLink"))
    68.         {
    69.             Debug.Log("Stopping");
    70.             CancelInvoke("CmdAnimateLink");
    71.             anime_Trigger = false;
    72.         }
    73.     }
    74.     public void Play_MoveAnimation()
    75.     {
    76.         CmdStop_MoveAnimation();
    77.         Debug.Log("Playing");
    78.         InvokeRepeating("CmdAnimateLink", 0, anime_Speed);
    79.  
    80.     }
    81.     [Command]
    82.     public void CmdPlay_MoveAnimation()
    83.     {
    84.         CmdStop_MoveAnimation();
    85.         Debug.Log("Playing2");
    86.         InvokeRepeating("CmdAnimateLink", 0, anime_Speed);
    87.  
    88.     }
    89.     [Command]
    90.     private void CmdAnimateLink()
    91.     {
    92.         Debug.Log("LinkINIT");
    93.         StartCoroutine("CmdAnimate");
    94.  
    95.     }
    96.    
    97.     public IEnumerator CmdAnimate()
    98.     {
    99.         Debug.Log("ENUMERATING");
    100.         switch (syncDirection)
    101.         {
    102.             case 0:
    103.                 yield return new WaitForSeconds(anime_Speed / 4);
    104.                 current_Sprite.sprite = anime_Frames[0];
    105.                 yield return new WaitForSeconds(anime_Speed / 4);
    106.                 current_Sprite.sprite = anime_Frames[1];
    107.                 yield return new WaitForSeconds(anime_Speed / 4);
    108.                 current_Sprite.sprite = anime_Frames[2];
    109.                 yield return new WaitForSeconds(anime_Speed / 4);
    110.                 current_Sprite.sprite = anime_Frames[1];
    111.                 yield return new WaitForSeconds(anime_Speed / 4);
    112.  
    113.                 break;
    114.  
    115.             case 1:
    116.                 yield return new WaitForSeconds(anime_Speed / 4);
    117.                 current_Sprite.sprite = anime_Frames[3];
    118.                 yield return new WaitForSeconds(anime_Speed / 4);
    119.                 current_Sprite.sprite = anime_Frames[4];
    120.                 yield return new WaitForSeconds(anime_Speed / 4);
    121.                 current_Sprite.sprite = anime_Frames[5];
    122.                 yield return new WaitForSeconds(anime_Speed / 4);
    123.                 current_Sprite.sprite = anime_Frames[4];
    124.                 yield return new WaitForSeconds(anime_Speed / 4);
    125.  
    126.                 break;
    127.             case 2:
    128.                 yield return new WaitForSeconds(anime_Speed / 4);
    129.                 current_Sprite.sprite = anime_Frames[6];
    130.                 yield return new WaitForSeconds(anime_Speed / 4);
    131.                 current_Sprite.sprite = anime_Frames[7];
    132.                 yield return new WaitForSeconds(anime_Speed / 4);
    133.                 current_Sprite.sprite = anime_Frames[8];
    134.                 yield return new WaitForSeconds(anime_Speed / 4);
    135.                 current_Sprite.sprite = anime_Frames[7];
    136.                 yield return new WaitForSeconds(anime_Speed / 4);
    137.  
    138.                 break;
    139.             case 3:
    140.                 yield return new WaitForSeconds(anime_Speed / 4);
    141.                 current_Sprite.sprite = anime_Frames[9];
    142.                 yield return new WaitForSeconds(anime_Speed / 4);
    143.                 current_Sprite.sprite = anime_Frames[10];
    144.                 yield return new WaitForSeconds(anime_Speed / 4);
    145.                 current_Sprite.sprite = anime_Frames[11];
    146.                 yield return new WaitForSeconds(anime_Speed / 4);
    147.                 current_Sprite.sprite = anime_Frames[10];
    148.                 yield return new WaitForSeconds(anime_Speed / 4);
    149.  
    150.                 break;
    151.         }
    152.     }
    153.  
    154.  
    155.     [Command]
    156.     void CmdProvideDirectionToServer(int direction)
    157.     {
    158.         syncDirection = direction; //feeding the server my current frame
    159.         if (syncDirection != 4 && anime_Trigger == false)
    160.         {
    161.             anime_Trigger = true;
    162.             CmdPlay_MoveAnimation();
    163.             Debug.Log(syncDirection.ToString() + "Transmitting");
    164.         }
    165.         else if (syncDirection == 4 && anime_Trigger == true)
    166.         {
    167.             CmdStop_MoveAnimation();
    168.         }
    169.     }
    170.  
    171.     [Client]
    172.     void TransmitDirectionChange()
    173.     {
    174.         if(isLocalPlayer)
    175.         {
    176.             CmdProvideDirectionToServer(anime_Direction); //ran on the client and tells server what to run with parameters
    177.             Debug.Log("local- " + netId.ToString() + "Sync var is " + syncDirection);
    178.          
    179.         }
    180.     }
    181.  
    182.    
    183. }
    184.  
     
  2. Denchyaknow

    Denchyaknow

    Joined:
    Aug 4, 2015
    Posts:
    31
    Nevermind seems like I solved it by writing a different script :p
     
  3. TehGM

    TehGM

    Joined:
    Nov 15, 2013
    Posts:
    89
    I recommend you posting the script here, so someone else can see if they have the same issue.
    Unless you want to keep your code secret, obviously.