Skip to main content
deleted 10 characters in body
Source Link
Big T Larrity
  • 1.4k
  • 4
  • 31
  • 61

Ok, well almost by trial-and-error I seem to have fixed this. I will post what I did here in case it ever helps anyone, but hopefully it is correct. All I know is the speed of the animation is now syncing on all screens (ie. The Host server-client and all other remote clients)

I had to use ClientRpc instead of Command, and then in Update i put if isServer, then a call to the Rpc I made:

public class Player_mp : NetworkBehaviour {
CharacterController characterCont;
Animator anim;
public float walkSpeed, sideSpeed, turnSpeed;
Vector3 movementInWorldSpace;


[SyncVar] float animVelZ;

public override void OnStartClient()
{
    characterCont = GetComponent<CharacterController>();
    anim = GetComponentInChildren<Animator>();
}

void FixedUpdate () {
    if (isServer)
    {
        RpcSetAnimSpeed();
    }

    if (!isLocalPlayer)
    {
        return;
    }



    float mouseX = Input.GetAxis("Mouse X");
    transform.SetPositionAndRotation(transform.position, Quaternion.Euler(transform.eulerAngles.x, transform.eulerAngles.y + (mouseX * turnSpeed * Time.deltaTime), transform.eulerAngles.z));

    Vector3 movement = new Vector3(Input.GetAxis("Horizontal") * walkSpeed * Time.deltaTime, 0, Input.GetAxis("Vertical") * sideSpeed * Time.deltaTime);
    movementInWorldSpace = transform.TransformVector(movement);



}

private void LateUpdate()
{
    if (!isLocalPlayer)
    {
        return;
    }

    characterCont.Move(movementInWorldSpace);
}

[ClientRpc]
void RpcSetAnimSpeed()
{
    Vector3 velLocalised = transform.InverseTransformVector(characterCont.velocity);
    //if (velLocalised != Vector3.zero) 
    //    Debug.Log("Vel = " + velLocalised);

    animVelZ = velLocalised.z;
    if (animVelZ < 0)
        animVelZ *= -1;
    anim.speed = animVelZ;
}

}

animSpeed , nor any of the variables used, needed to by a SyncVar

Ok, well almost by trial-and-error I seem to have fixed this. I will post what I did here in case it ever helps anyone, but hopefully it is correct. All I know is the speed of the animation is now syncing on all screens (ie. The Host server-client and all other remote clients)

I had to use ClientRpc instead of Command, and then in Update i put if isServer, then a call to the Rpc I made:

public class Player_mp : NetworkBehaviour {
CharacterController characterCont;
Animator anim;
public float walkSpeed, sideSpeed, turnSpeed;
Vector3 movementInWorldSpace;


[SyncVar] float animVelZ;

public override void OnStartClient()
{
    characterCont = GetComponent<CharacterController>();
    anim = GetComponentInChildren<Animator>();
}

void FixedUpdate () {
    if (isServer)
    {
        RpcSetAnimSpeed();
    }

    if (!isLocalPlayer)
    {
        return;
    }



    float mouseX = Input.GetAxis("Mouse X");
    transform.SetPositionAndRotation(transform.position, Quaternion.Euler(transform.eulerAngles.x, transform.eulerAngles.y + (mouseX * turnSpeed * Time.deltaTime), transform.eulerAngles.z));

    Vector3 movement = new Vector3(Input.GetAxis("Horizontal") * walkSpeed * Time.deltaTime, 0, Input.GetAxis("Vertical") * sideSpeed * Time.deltaTime);
    movementInWorldSpace = transform.TransformVector(movement);



}

private void LateUpdate()
{
    if (!isLocalPlayer)
    {
        return;
    }

    characterCont.Move(movementInWorldSpace);
}

[ClientRpc]
void RpcSetAnimSpeed()
{
    Vector3 velLocalised = transform.InverseTransformVector(characterCont.velocity);
    //if (velLocalised != Vector3.zero) 
    //    Debug.Log("Vel = " + velLocalised);

    animVelZ = velLocalised.z;
    if (animVelZ < 0)
        animVelZ *= -1;
    anim.speed = animVelZ;
}

}

Ok, well almost by trial-and-error I seem to have fixed this. I will post what I did here in case it ever helps anyone, but hopefully it is correct. All I know is the speed of the animation is now syncing on all screens (ie. The Host server-client and all other remote clients)

I had to use ClientRpc instead of Command, and then in Update i put if isServer, then a call to the Rpc I made:

public class Player_mp : NetworkBehaviour {
CharacterController characterCont;
Animator anim;
public float walkSpeed, sideSpeed, turnSpeed;
Vector3 movementInWorldSpace;


float animVelZ;

public override void OnStartClient()
{
    characterCont = GetComponent<CharacterController>();
    anim = GetComponentInChildren<Animator>();
}

void FixedUpdate () {
    if (isServer)
    {
        RpcSetAnimSpeed();
    }

    if (!isLocalPlayer)
    {
        return;
    }



    float mouseX = Input.GetAxis("Mouse X");
    transform.SetPositionAndRotation(transform.position, Quaternion.Euler(transform.eulerAngles.x, transform.eulerAngles.y + (mouseX * turnSpeed * Time.deltaTime), transform.eulerAngles.z));

    Vector3 movement = new Vector3(Input.GetAxis("Horizontal") * walkSpeed * Time.deltaTime, 0, Input.GetAxis("Vertical") * sideSpeed * Time.deltaTime);
    movementInWorldSpace = transform.TransformVector(movement);



}

private void LateUpdate()
{
    if (!isLocalPlayer)
    {
        return;
    }

    characterCont.Move(movementInWorldSpace);
}

[ClientRpc]
void RpcSetAnimSpeed()
{
    Vector3 velLocalised = transform.InverseTransformVector(characterCont.velocity);
    //if (velLocalised != Vector3.zero) 
    //    Debug.Log("Vel = " + velLocalised);

    animVelZ = velLocalised.z;
    if (animVelZ < 0)
        animVelZ *= -1;
    anim.speed = animVelZ;
}

}

animSpeed , nor any of the variables used, needed to by a SyncVar

Source Link
Big T Larrity
  • 1.4k
  • 4
  • 31
  • 61

Ok, well almost by trial-and-error I seem to have fixed this. I will post what I did here in case it ever helps anyone, but hopefully it is correct. All I know is the speed of the animation is now syncing on all screens (ie. The Host server-client and all other remote clients)

I had to use ClientRpc instead of Command, and then in Update i put if isServer, then a call to the Rpc I made:

public class Player_mp : NetworkBehaviour {
CharacterController characterCont;
Animator anim;
public float walkSpeed, sideSpeed, turnSpeed;
Vector3 movementInWorldSpace;


[SyncVar] float animVelZ;

public override void OnStartClient()
{
    characterCont = GetComponent<CharacterController>();
    anim = GetComponentInChildren<Animator>();
}

void FixedUpdate () {
    if (isServer)
    {
        RpcSetAnimSpeed();
    }

    if (!isLocalPlayer)
    {
        return;
    }



    float mouseX = Input.GetAxis("Mouse X");
    transform.SetPositionAndRotation(transform.position, Quaternion.Euler(transform.eulerAngles.x, transform.eulerAngles.y + (mouseX * turnSpeed * Time.deltaTime), transform.eulerAngles.z));

    Vector3 movement = new Vector3(Input.GetAxis("Horizontal") * walkSpeed * Time.deltaTime, 0, Input.GetAxis("Vertical") * sideSpeed * Time.deltaTime);
    movementInWorldSpace = transform.TransformVector(movement);



}

private void LateUpdate()
{
    if (!isLocalPlayer)
    {
        return;
    }

    characterCont.Move(movementInWorldSpace);
}

[ClientRpc]
void RpcSetAnimSpeed()
{
    Vector3 velLocalised = transform.InverseTransformVector(characterCont.velocity);
    //if (velLocalised != Vector3.zero) 
    //    Debug.Log("Vel = " + velLocalised);

    animVelZ = velLocalised.z;
    if (animVelZ < 0)
        animVelZ *= -1;
    anim.speed = animVelZ;
}

}