0
votes

I am using this code on a photon view(c#) in the Unity game engine/Photon unity networking. For the purpose of networking animations and other stuff however for some reason the code just won't network my animations.

using UnityEngine;
using System.Collections;

public class NetworkCharacter : Photon.MonoBehaviour {

    Vector3 realPos = Vector3.zero;
    Quaternion realRot = Quaternion.identity;
    Animator anim;
    bool gotFU = false;

    // Use this for initialization
    void Start () {
        anim = GetComponent<Animator> ();
    }

    // Update is called once per frame
    void Update () {
        if (photonView.isMine) 
        {

        } 
        else 
        {
            transform.position = Vector3.Lerp (transform.position, realPos, 0.1f);
            transform.rotation = Quaternion.Lerp (transform.rotation, realRot, 0.1f);
        }
    }

    public void OnPhotonSerializeView(PhotonStream stream, PhotonMessageInfo info)
    {
        if (stream.isWriting) 
        {
            stream.SendNext (transform.position);
            stream.SendNext (transform.rotation);
            stream.SendNext (anim.GetFloat("Speed"));
            stream.SendNext (anim.GetFloat("Strafe"));
        } 
        else 
        {
            realPos = (Vector3)stream.ReceiveNext ();
            realRot = (Quaternion)stream.ReceiveNext ();
            anim.SetFloat("Speed", (float)stream.ReceiveNext());
            anim.SetFloat("Strafe", (float)stream.ReceiveNext());


            if (gotFU == false) 
            {
                transform.position = realPos;
                transform.rotation = realRot;
                gotFU = true;
            }


        }


    }
}
1

1 Answers

0
votes

To synchronize position and rotation you can use Photon View and Photon Transform View component.

Please check it out: Serialization in Photon