So far everything I’ve tried breaks something else or creates another problem, so any help is appreciated.
When I run this with a host and a client, the animator performs everything properly (such as idle animation and running animation) on both systems. However when the host player inputs any movement command when the client player isn’t moving, the client model also moves with the host but much slower.
Does anyone have any diea on what the problem is? Here’s my PlayerMovement.cs:
using UnityEngine;
using System.Collections;
using UnityEngine.Networking;
public class PlayerMovement : NetworkBehaviour
{
private Animator anim;
private Rigidbody2D myRigidbody;
public float runSpeed;
[Command]
void CmdWalk(bool isWalking, float input_x, float input_y, float x, float y)
{
RpcWalk(isWalking, input_x, input_y, x, y);
}
[ClientRpc]
void RpcWalk(bool isWalking, float input_x, float input_y, float x, float y)
{
if (isLocalPlayer)
return;
anim.SetBool("isWalking", isWalking);
anim.SetFloat("x", x); //animation direction based on mouse /joystick position
anim.SetFloat("y", y);
if (isWalking) //if walking, translate player
{
//transform.position += new Vector3(input_x, input_y, 0).normalized * Time.deltaTime * runSpeed;
myRigidbody.velocity = new Vector2(input_x, input_y).normalized * runSpeed;
}
else
{
myRigidbody.velocity = new Vector2(0, 0);
}
}
// Use this for initialization
void Start () {
anim = GetComponent<Animator>();
myRigidbody = GetComponent<Rigidbody2D>();
}
// Update is called once per frame
void Update () {
var mousePos = Input.mousePosition;
mousePos.x -= Screen.width / 2;
mousePos.y -= Screen.height / 2;
float input_x = Input.GetAxisRaw("Horizontal");
float input_y = Input.GetAxisRaw("Vertical");
bool isWalking = (Mathf.Abs(input_x) + Mathf.Abs(input_y)) > 0;
bool isStat = (Mathf.Abs(input_x) + Mathf.Abs(input_y) + Mathf.Abs(mousePos.x) + Mathf.Abs(mousePos.y)) > 0;
CmdWalk(isWalking, input_x, input_y, mousePos.x, mousePos.y);
if (isLocalPlayer)
{
anim.SetBool("isWalking", isWalking);
anim.SetFloat("x", mousePos.x); //animation direction based on mouse /joystick position
anim.SetFloat("y", mousePos.y);
if (isWalking) //if walking, translate player
{
//transform.position += new Vector3(input_x, input_y, 0).normalized * Time.deltaTime * runSpeed;
myRigidbody.velocity = new Vector2(input_x, input_y).normalized * runSpeed;
}
else
{
myRigidbody.velocity = new Vector2(0, 0);
}
}
}
}