Sync Mechanim animation with OnPhotonSerializeView

Hello,

I have touble syncronizing data of Animator component via the callback OnPhotonSerializeView().. I am able to see the transform change and tried the same way with the animator component.

Check the code here:

[code2=csharp]void OnPhotonSerializeView(PhotonStream stream, PhotonMessageInfo info)
{
if (stream.isWriting)
{
stream.SendNext(transform.position);
stream.SendNext(transform.rotation);

stream.SendNext(this.m_animator);
}
else
{
this.correctPlayerPos = (Vector3)stream.ReceiveNext();
this.correctPlayerRot = (Quaternion)stream.ReceiveNext();

this.m_animator = (Animator)stream.ReceiveNext();
}
}[/code2]
this code is in the script "Network Character" attached to my third person character.

And it gives this error: Exception: cannot serialize(): UnityEngine.Animator

I comment them (leave position and rotation) only, and the error is gone.

Also I thought of this.m_animator.animation = (Animation)stream.ReceiveNext(); but cant do since (UnityEngine.Component.animation' cannot be assigned to (it is read only))

Any idea of how to syncronize the animations from the animator component?

Comments

  • You can't send an Animator instance through Photon. You have to grab relevant data from it and send it with the known datatypes.

    Check "Serializable Types" in this page:
    http://doc.exitgames.com/en/photon-serv ... ryProtocol

    In addition, PUN registers Vector3, Vector2, Quaternion and PhotonPlayer, making them send-able in a stream.

    I don't know the Animator component first hand, so I can't help with details how to fetch stuff from it.
    I would recommend not trying to sync all animations but instead just sync the actions and then trigger animations on all clients. So when you jump, send that and not "i am jumping and you have to use this animation".
  • Thank you for your answer :)

    However I tried this as well:

    [code2=csharp]void FixedUpdate()
    {
    m_animator.SetBool("Run" , runBool);
    m_animator.SetBool("Idle" , idleBool);
    }

    void OnPhotonSerializeView(PhotonStream stream, PhotonMessageInfo info)
    {
    if (stream.isWriting)
    {
    stream.SendNext(transform.position);
    stream.SendNext(transform.rotation);
    stream.SendNext(runBool);
    stream.SendNext(idleBool);
    }
    else
    {
    this.correctPlayerPos = (Vector3)stream.ReceiveNext();
    this.correctPlayerRot = (Quaternion)stream.ReceiveNext();
    idleBool = (bool)stream.ReceiveNext();
    runBool = (bool)stream.ReceiveNext();
    }
    }[/code2]

    I have no errors but still animations are not updated.
  • Have you tried from an Update() method ?

    Also, setting your animator bool value does not reset the animation Keyframe (i don't know) ?
    Maybe you can try something like :

    [code2=csharp]void FixedUpdate()
    {
    if (m_animator.GetBool("Run") != runBool)
    m_animator.SetBool("Run" , runBool);
    if (m_animator.GetBool("Idle") != idleBool)
    m_animator.SetBool("Idle" , idleBool);
    }[/code2]

    But maybe it's because you don't verify is the photonView is yours ?
    Taking your original code it would be :

    [code2=csharp]void FixedUpdate()
    {
    if (!photonView.isMine)
    {
    m_animator.SetBool("Run" , runBool);
    m_animator.SetBool("Idle" , idleBool);
    }
    }[/code2]

    In my project i don't have theses type of problem with animation, but because my gameRules are relatively simple, i juste update some InGames Event with RPC instead of using OnPhotonSerializeView method.
  • Hi,
    http://www.youtube.com/watch?v=4AZADiKD2sE
    Check this out ! I had the same problem and this video helped me to solve it ! Hope this helps ! :D

    Regards,
    Razor.
  • Great thanks !!
    great video, solved :D