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).
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.