- Home /
Sync Network Animation State
How do i go about having animations show in my online game. i have the same prefab for each player and you can only see thier position,rotation,scale but animations don't show over the network they only show localy on the client meaning each client can only see his own anitmation but not other's.
i've looked it up on the web and i found out that i need to make some kind of enum that will comtain the player's current animation and then broadcast it or something, it's called Network animation Sync if i'm not mistaken, but the problem is i have no idea of how to do that or how to it works.
if some1 could point me in the right direction or give me some kind of script it wouyld be wonderful.
thanks for reading. (BTW i'm using C#)
Answer by toddisarockstar · Apr 24, 2015 at 08:06 PM
i asked this a while ago too. the answer is "you dont". lol 90% of the time its not nessessarry. each players script can determain for itself.
for example: your existing script can simply detect if a transform.position hasnt changed in a few frames. so your script can assume to trigger a "standing" animation without sync.
if transform.position HAS changed from the last frame your individual machines would each trigger its own "walk" animation without networkhelp.
if you send an rpc for an object to shoot something. then no need for an additional animation rpc. just trigger the "shoot" animation along with the shooting.
for unusual situations. you can always RPC the animation but even if you could "Sync" automatically it would clog precius bandwidth that you need for multiplayer.
these few extra lines of detection is far more efficient than clogging up your small precious bandwidth to sync a bunch of animations.
data proccessed through the internet is like a pinhole compareded to what any modern CPU can calculate. General rule for proficancy for online games is only send what you really have to!
actually this way looks smoother too. the couple frames delay it takes for a pc to figure out if a character is moving is a unnoticable fration of a second. its faster than the time it takes to get a data through the internet. there is no "lag".
but to reluctantly answer your question..... if you really really wanted a super slow laggy game you could stick seperate networkveiw components with state syncs on all the CHILD objects of your objects and this would actually sync your animations like are describing. you would just need on EVERY object part.
Answer by Mikdad_Merchant · Apr 24, 2015 at 07:46 PM
well in a game I was working on I just need to solve the same issue, this script is what is guiding me, this is provided by @Mike Gieg from Unity in the "Merry Fragmas" Tutorial series. See the part where the animator parameters are set in the coroutine and onphotonserializeview the information is received, this is the key to solving your problem. Hope it helps. Also I highly recommend seeing part three of the merry fragmas series or was it part two well not sure, anyways you should see the whole thing its simple awesome.
This script is using Photon Unity Network, but if you using something else then your code will have to be modified accordingly, all the best.
[code] using UnityEngine; using System.Collections;
public class PlayerNetworkMover : Photon.MonoBehaviour { public delegate void Respawn(float time); public event Respawn RespawnMe; public delegate void SendMessage(string MessageOverlay); public event SendMessage SendNetworkMessage;
Vector3 position;
Quaternion rotation;
float smoothing = 10f;
float health = 100f;
bool aim = false;
bool sprint = false;
bool initialLoad = true;
Animator anim;
void Start()
{
anim = GetComponentInChildren<Animator> ();
if(photonView.isMine)
{
transform.Find ("Head Joint/First Person Camera/GunCamera/Candy-Cane").gameObject.layer = 11;
transform.Find ("Head Joint/First Person Camera/GunCamera/Candy-Cane/Sights").gameObject.layer = 11;
GetComponent<Rigidbody> ().useGravity = true;
GetComponent<FirstPersonCharacter> ().enabled = true;
GetComponent<FirstPersonHeadBob> ().enabled = true;
GetComponent<SimpleMouseRotator> ().enabled = true;
GetComponentInChildren<PlayerShooting> ().enabled = true;
GetComponentInChildren<AudioListener> ().enabled = true;
foreach(SimpleMouseRotator rot in GetComponentsInChildren<SimpleMouseRotator> ())
rot.enabled = true;
foreach(Camera cam in GetComponentsInChildren<Camera>())
cam.enabled = true;
}
else
{
StartCoroutine("UpdateData");
}
}
IEnumerator UpdateData ()
{
if(initialLoad)
{
initialLoad = false;
transform.position = position;
transform.rotation = rotation;
}
while(true)
{
transform.position = Vector3.Lerp(transform.position, position, Time.deltaTime * smoothing);
transform.rotation = Quaternion.Lerp(transform.rotation, rotation, Time.deltaTime * smoothing);
anim.SetBool("Aim", aim);
anim.SetBool ("Sprint", sprint);
yield return null;
}
}
void OnPhotonSerializeView(PhotonStream stream, PhotonMessageInfo info)
{
if (stream.isWriting)
{
stream.SendNext(transform.position);
stream.SendNext(transform.rotation);
stream.SendNext(health);
stream.SendNext(anim.GetBool ("Aim"));
stream.SendNext(anim.GetBool ("Sprint"));
}
else
{
position = (Vector3)stream.ReceiveNext();
rotation = (Quaternion)stream.ReceiveNext();
health = (float)stream.ReceiveNext();
aim = (bool)stream.ReceiveNext();
sprint = (bool)stream.ReceiveNext();
}
}
[RPC]
public void GetShot(float damage, string enemyName)
{
health -= damage;
if (health <= 0 && photonView.isMine){
if(SendNetworkMessage != null)
SendNetworkMessage(PhotonNetwork.player.name + " was killed by " + enemyName);
if(RespawnMe != null)
RespawnMe(3f);
PhotonNetwork.Destroy (gameObject);
}
}
}1
[/code]
Your answer
![](https://koobas.hobune.stream/wayback/20220613072332im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Sync animaor of child prefab 0 Answers
Network - Sync Issue 0 Answers
Network Animation Problem 1 Answer
Multiple Cars not working 1 Answer
Network animations 2 Answers