Hey guys,
I’m trying to get animations to sync properly in my Unet multiplayer game. Right now, they work in most cases, but not in all cases. And I’m trying to get them to where they are reliable, and I don’t see failures in playing certain animations at certain times – which I currently do.
First attempt:
I have tried using the “Network Animator” component that comes by default. What I noticed was that my mecanim variables sync properly IF my character/agent does NOT have “LocalPlayerAuthority” checked on their Network Identity. If it is checked, then the Network Animator variables do nothing at all, and don’t seem to be sent properly.
This is problematic because I’m mainly using this function to spawn objects:
NetworkServer.SpawnWithClientAuthority(go, connectionToClient);
The only way to get animations to sync automatically is to NOT have “LocalPlayerAuthority” checked. As per the documentation though: “Non-player objects that are to have client authority must have LocalPlayerAuthority checked in their NetworkIdentity.” But that is also what causes the Network Animator to fail – so that is a dilemma, and seems to be at odds with each other.
Second attempt:
Since the Network Animator doesn’t seem to work in my case, I have tried doing it manually. And this is what kind of (sort of), in some cases, seems to work – a little bit lol. But I think I need help with it. I’m currently using this code (this is one example but I’m using it for all of my animation states currently):
//If the enemy target dies, do the following
if (enemyTarget.GetComponent<HealthAndDeath>().currentHealth <= 0)
{
isInAttackState = false;
//Stop the unit from continuing to the enemy target's location after the enemy target dies.
//I'm using "currentWaypoint" instead of "path = null", because the code to stop the unit's
//run animation is only in the currentWaypoint condition check in the Move script.
moveScript.currentWaypoint = 15000;
//Stop playing the attack animation on this unit (and command the server and other
//clients to do so as well in multiplayer)
if (thisAnimator.GetBool("playAttackAnimation1") == true)
{
thisAnimator.SetBool("playAttackAnimation1", false);
//Tell the server instance of this unit to stop the attack animation
CmdStopAttackAnimation1();
//Debug.Log("I'm sending a command to the server");
if (isServer)
{
//Tell other clients to stop the attack animation on this unit
RpcStopAttackAnimation1();
}
//Debug.Log("I'm sending commands to server to stop the attack animation");
}
enemyTarget = null;
}
[Command]
public void CmdStopAttackAnimation1()
{
NetworkServer.FindLocalObject(thisNetworkIdentity.netId).GetComponent<Animator>().SetBool("playAttackAnimation1", false);
}
[ClientRpc]
public void RpcStopAttackAnimation1()
{
thisAnimator.SetBool("playAttackAnimation1", false);
}
I’d like to know if I might be doing something majorly wrong here or not? Because if my code actually looks fairly good, then perhaps it’s some of my other behavioral states or conditions that I am failing to sync properly.
Thanks for any help or ideas.