1

I'm trying to learn about Unet, but I'm having trouble getting the hang of it, despite following a few tutorials.

I am making a 2d game, and what I'm currently stuck at, is updating the way my characters sprite is turning (left or right).

Problem shown here

My player prefab has: Sprite renderer, rigidbody2D, Network Identity(set to local player authority) and Network Transform.

This is the code attached to each player:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Networking;

public class CharController : NetworkBehaviour {

    public float maxSpeed = 1f;
    [SyncVar]
    bool facingRight = true;
    Animator anim;


    // Use this for initialization
    void Start () {
        anim = GetComponent<Animator>();
    }

    // Update is called once per frame
    void Update () {

    }
    void Moving()
    {


            float move = Input.GetAxis("Horizontal");

            GetComponent<Rigidbody2D>().velocity = new Vector2(move * maxSpeed, GetComponent<Rigidbody2D>().velocity.y);

            if(move > 0 && !facingRight)
            {
                RpcFlip();
            }
            else if(move < 0 && facingRight)
            {
                RpcFlip();
            }
            anim.SetFloat("MovingSpeed", Mathf.Abs(move));
    }

    private void FixedUpdate()
    {
        if (!isLocalPlayer)
        {
            return;
        }
        // handle input here...
        Moving();







    }
    [ClientRpc]
    void RpcFlip()
    {
        if (isLocalPlayer)
        {
            //facingRight = !facingRight;
            //currentSprite.flipX = !currentSprite.flipX;


        }

        facingRight = !facingRight;
            Vector3 theScale = transform.localScale;
            theScale.x *= -1;
            transform.localScale = theScale;

    }
}

I know it's most likely something super simple I'm doing wrong, which just adds more pain to asking this, but any help is greatly appreciated! Thanks

Edit: The way my game is setup, one of the players is both a host and a client, I don't know if this makes it harder, but that is just the way unity does it, when playing locally.

Fross
  • 122
  • 13
  • A good tip is to put your client and server code in to *totally different files*. You often see example code where it is shoehorned in the same file. It's actually easier and more logical for beginners if you just use two separate files. – Fattie Feb 19 '17 at 15:33
  • Thanks, but an attempted answer at my question would've been more helpful ;) – Fross Feb 19 '17 at 19:26
  • fross - fair enough, but I just couldn't be bothered. and I knew you'd figure it out :) – Fattie Feb 19 '17 at 20:52

1 Answers1

0

I figured it out after reading the first 5 pages on google, and here is my final code, in case anyone else with the same problem stumbles in here:

public float maxSpeed = 1f;
    Animator anim;

    [SyncVar(hook = "FacingCallback")]  //Everytime the bool netFacingRight is called, the method FacingCallback is called aswell. 
                                        //Since the bool only changes when CmdFlipSprite is called, it makes sure that CmdFlipSprite calls
                                        //the change on our server, and FacingCallback calls it locally.
    public bool netFacingRight = true;

    // Use this for initialization
    void Start () {
        anim = GetComponent<Animator>();
    }


    [Command]
    public void CmdFlipSprite(bool facing)
    {
        netFacingRight = facing;
        if (netFacingRight)
        {
            Vector3 SpriteScale = transform.localScale;
            SpriteScale.x = 1;
            transform.localScale = SpriteScale;
        }
        else
        {
            Vector3 SpriteScale = transform.localScale;
            SpriteScale.x = -1;
            transform.localScale = SpriteScale;
        }
    }

    void FacingCallback(bool facing)
    {
        netFacingRight = facing;
        if (netFacingRight)
        {
            Vector3 SpriteScale = transform.localScale;
            SpriteScale.x = 1;
            transform.localScale = SpriteScale;
        }
        else
        {
            Vector3 SpriteScale = transform.localScale;
            SpriteScale.x = -1;
            transform.localScale = SpriteScale;
        }
    }

    ////////




    // Update is called once per frame
    void Update () {

        if (!isLocalPlayer)
        {
            return;
        }
        float move = Input.GetAxis("Horizontal");

        GetComponent<Rigidbody2D>().velocity = new Vector2(move * maxSpeed, GetComponent<Rigidbody2D>().velocity.y);

        if ((move > 0 && !netFacingRight) || (move < 0 && netFacingRight))
        {
            netFacingRight = !netFacingRight;
            CmdFlipSprite(netFacingRight);
            anim.SetFloat("MovingSpeed", Mathf.Abs(move));
        }
    }

You aparently have to call the function both locally and on the server!

Fross
  • 122
  • 13