Mecanim with Photon - Works but laggy?

Options
Hey!

I recently succeeded with making animation work with photon by sending the animation parameters mecanim needs to the other clients, then mecanim does its logic on its own to sort what animations to show. My code works, but the game starts lagging quite alot when another player then myself enters the game. Note that it does not lag at all when i'm the only person in the game.

Here is my code:
using UnityEngine;
using System.Collections;

public class ThirdPersonNetwork : Photon.MonoBehaviour
{


		
		Animator anim;
		Vector3 correctPlayerPos = Vector3.zero; //We lerp towards this
		Quaternion correctPlayerRot = Quaternion.identity; //We lerp towards this
		float correctPlayerSpeed = 0;
		
		

		void Awake ()
		{
		
				anim = gameObject.GetComponent<Animator> ();

				gameObject.name = gameObject.name + photonView.viewID;
		}

		void Update ()
		{
		
				if (!photonView.isMine) {
						
						transform.position = Vector3.Lerp (transform.position, correctPlayerPos, Time.deltaTime * 5);
						transform.rotation = Quaternion.Lerp (transform.rotation, correctPlayerRot, Time.deltaTime * 5);
						anim.SetFloat ("Speed", correctPlayerSpeed);
			
				}
		}

		void OnPhotonSerializeView (PhotonStream stream, PhotonMessageInfo info)
		{
				if (stream.isWriting) {
						//We own this player: send the others our data
						
						stream.SendNext (transform.position);
						stream.SendNext (transform.rotation); 
						stream.SendNext ((float)anim.GetFloat ("Speed"));


				} else {
						//Network player, receive data
            
						correctPlayerPos = (Vector3)stream.ReceiveNext ();
						correctPlayerRot = (Quaternion)stream.ReceiveNext ();
						correctPlayerSpeed = (float)stream.ReceiveNext ();



				}
		}

}

Comments

  • Tobias
    Options
    I didn't see anything suspicous in the script itself. Seems to be fine.
    You might want to check which region you connect to. There are several for the Photon Cloud and maybe you never picked the one close to you?

    In main menu, window, Photon Unity Networking, you find a "setup" button and can pick a region.