NetworkAnimator not working for non-player objects (755391)

Greetings!

I have my a Prefab named MaleSprite and set it as a Player Prefab under the Spawn Info in Network Manager which MaleSprite has Network Identity with Local Player Authority, Network Transform, Network Animator checked parameters and PlayerController script, everything works fine.

Now I have my 2nd prefab named Weapon and set it under the Registered Spawnable Prefabs in Network Manager which Weapon has Network Identity with Local Player Authority, Network Transform and Network Animator and checked parameters.

Basically, on PlayerController Script, it first instantiates the Weapon prefab and set its parent on MaleSprite then when when MaleSprite moves, Weapon moves and transforms to all the players, which is Good. However, when a player presses Left Click, it will send the bool value True to Weapon.animator.setBool(“Attack”, attack) which is attached to the Weapon prefab. It is visible on the local player, however animation doesn’t appear for other players, which is Bad.

I have tried this methods:

  1. NetworkServer.SpawnWithClientAuthority(weapon, connectionToClient);
    Problem: It works for the Host only, like when I attack, it animates to all the clients, but maintain in the current transform position, and spawns another one in the default transform position also, when other local player attacks (not host) it doesn’t animate to all the clients.

  2. Instead of on Player Controller as weapon.animator.setBool(“Attack”, attack); Meaning sending the state directly to the attached animator component of the Weapon. I made it on Player Controller as weapon.attackState(attack); wherein on Weapon Script it receives the value as public void attackState(bool attack){ status = attack; } then on void Update(){ animator.setBool(“Attack”, status); } Meaning I’ve sent the bool value to another method inside Weapon Script to change the state on Animator attached on Weapon prefab. It animates as well on the local player, but it doesn’t animate to all the clients.

  3. Setting Weapon as a Player Prefab under Spawn Info in Network Manager, it works perfectly, it animates to all the client, however it replaces the original Male Sprite and the same event will occur with Male Sprite when instantiated.

  4. Adding [SyncVar] at the top of status variable inside the Weapon Script.

  5. Making a method of
    [Command]
    void CmdAttack() {
    attack = true;
    }

  6. Unchecked Local Player Authority.

None of them worked. Any workarounds?

public void Update()
{
   if(Input.GeyKeyDown(KeyCode.Given)
   {
      CmdAttackPlayers();
   }
}

[Command]
public void CmdAttackPlayers()
{
   RpcAttackPlayers();
}

[ClientRpc]
public void RpcAttackPlayers()
{
   ---> AttackPlayerAnimation here <---
}

or

[Command]
public void CmdAttackPlayer()
{
   attackPlayer = true;
}

Cliend side hook;

[SyncVar(hook = "PlayerAttackChanged")] public bool attackPlayer;

   public void PlayerAttackChanged(bool attackPlayer)
   {
      this.attackPlayer = attack;
      AttackMethod();
   }
}

Thank you for the reply!

I have implemented your code.
What happened is, it works really great when you done it with Host that is Server side only, and when it comes to the client side, there are bugs that occurred like delayed animation, weapon in the middle(original transform position) and sort of vibration on the weapon as show on the screenshot.

Did i miss, disarrange, forgot something? Here’s the complete code for Player Controller:
using UnityEngine;
using System.Collections;
using UnityEngine.Networking;

public class PlayerScript : NetworkBehaviour {

public float moveSpeed, attackSpeed;

bool up, down, left, right, walk, attack;
float attackTime;
GameObject weapon;
Transform plyrTrnsfrm;

void Start () {
plyrTrnsfrm = this.transform;
weapon = (GameObject)Instantiate (Resources.Load (“Prefabs/Weapons/TestWeapon”));
weapon.transform.parent = this.transform;
NetworkServer.Spawn (weapon);
attackSpeed = weapon.GetComponent ().weaponSpeed ();
}

[Command]
void CmdAttack(bool status){
RpcAttack (status);
}

[ClientRpc]
void RpcAttack(bool status){
weapon.GetComponent ().SetBool (“Attack”, status);
}

void Update () {
if(!isLocalPlayer){
return;
}
if (Input.GetKey (KeyCode.W)) {
plyrTrnsfrm.Translate (Vector3.up * moveSpeed * Time.deltaTime);
up = true;
down = false;
if (Input.GetKey (KeyCode.A)) {
plyrTrnsfrm.Translate (Vector3.left * moveSpeed * Time.deltaTime);
left = true;
left = true;
right = false;
} else if (Input.GetKey (KeyCode.D)) {
plyrTrnsfrm.Translate (Vector3.right * moveSpeed * Time.deltaTime);
left = false;
right = true;
} else {
left = false;
right = false;
}
walk = true;
} else if (Input.GetKey (KeyCode.S)) {
plyrTrnsfrm.Translate (Vector3.down * moveSpeed * Time.deltaTime);
up = false;
down = true;
if (Input.GetKey (KeyCode.A)) {
plyrTrnsfrm.Translate (Vector3.left * moveSpeed * Time.deltaTime);
left = true;
left = true;
right = false;
} else if (Input.GetKey (KeyCode.D)) {
plyrTrnsfrm.Translate (Vector3.right * moveSpeed * Time.deltaTime);
left = false;
right = true;
} else {
left = false;
right = false;
}
walk = true;
} else if (Input.GetKey (KeyCode.A)) {
plyrTrnsfrm.Translate (Vector3.left * moveSpeed * Time.deltaTime);
left = true;
right = false;
if (Input.GetKey (KeyCode.W)) {
plyrTrnsfrm.Translate (Vector3.up * moveSpeed * Time.deltaTime);
up = true;
down = false;
} else if (Input.GetKey (KeyCode.S)) {
plyrTrnsfrm.Translate (Vector3.down * moveSpeed * Time.deltaTime);
up = false;
down = true;
} else {
up = false;
down = false;
}
walk = true;
} else if (Input.GetKey (KeyCode.D)) {
plyrTrnsfrm.Translate (Vector3.right * moveSpeed * Time.deltaTime);
left = false;
right = true;
if (Input.GetKey (KeyCode.W)) {
plyrTrnsfrm.Translate (Vector3.up * moveSpeed * Time.deltaTime);
up = true;
down = false;
} else if (Input.GetKey (KeyCode.S)) {
plyrTrnsfrm.Translate (Vector3.down * moveSpeed * Time.deltaTime);
up = false;
down = true;
} else {
up = false;
down = false;
}
walk = true;
} else {
walk = false;
}
if ((Time.time - attackTime) > attackSpeed && Input.GetKey (KeyCode.Z)) {
attackTime = Time.time;
CmdAttack (true);
} else {
CmdAttack(false);
}
}
}

EDIT: Removed Network.Spawn(weapon); and it removes the glitch where it spawns in the middle and the vibrating effect in the animation, all that’s left is the delay in the synchronization of the animation across all the clients. :smile:

2619679--183910--ss.png

Problem Solved.

How did you solve the delay?