- Home /
Photon Syncing Animations
I am using the basic myThirdPersonController script as my movement and animation script. The movements are syncing when i play but the animations are not.
Heres my NetworkCharacter script: using UnityEngine;
public class NetworkCharacter : Photon.MonoBehaviour
{
private Vector3 correctPlayerPos; // We lerp towards this
private Quaternion correctPlayerRot; // We lerp towards this
// Update is called once per frame
void Update()
{
correctPlayerPos = transform.position;
correctPlayerRot = transform.rotation;
if (!photonView.isMine)
{
transform.position = Vector3.Lerp(transform.position, this.correctPlayerPos, Time.deltaTime * 5);
transform.rotation = Quaternion.Lerp(transform.rotation, this.correctPlayerRot, Time.deltaTime * 5);
}
}
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);
myThirdPersonController myC = GetComponent<myThirdPersonController>();
stream.SendNext((int)myC._characterState);
}
else
{
// Network player, receive data
this.correctPlayerPos = (Vector3)stream.ReceiveNext();
this.correctPlayerRot = (Quaternion)stream.ReceiveNext();
myThirdPersonController myC = GetComponent<myThirdPersonController>();
myC._characterState = (CharacterState)stream.ReceiveNext();
}
}
}
I have no idea what i have wrong in the script and need help!!!!
Answer by Flassari · Oct 13, 2014 at 03:56 PM
Make sure that you are actually setting the animation in the myThirdPersonController class. If should be checking what _characterState is and setting the animation from that, perhaps somewhere in Update() or a function that is called from Update().
Answer by verenion · Jan 27, 2015 at 02:15 PM
Just as a heads up, this is a naive way of syncing animations, especially if it's just for movement (idle, walk, run etc.).
What you should do, is sync the position and rotation, as you are. Then in update, you can extrapolate what animation needs to be used, based on the velocity.
E.g.
Vector3 lastPos;
Quaternion lastRot;
void Update()
{
correctPlayerPos = transform.position;
correctPlayerRot = transform.rotation;
if (!photonView.isMine)
{
transform.position = Vector3.Lerp(transform.position, this.correctPlayerPos, Time.deltaTime * 5);
transform.rotation = Quaternion.Lerp(transform.rotation, this.correctPlayerRot, Time.deltaTime * 5);
// get the velocity of the remote player
Vector3 vel = correctPlayerPos - this.lastPos;
// give this value to an animator / animation controller to set the correct animation.
animator.SetFloat("Speed", vel);
}
}
this way, the client is just getting the new position, and figuring out what motion animations to play based on the velocity vector. It might seem silly, but sending an animation state constantly will add up to a fair bit of bandwidth. It can also make animations seem more natural. Obviously the code above is a very rough idea.
UPDATE
You are also not setting the right data.
//you are sending as an int.
stream.SendNext((int)myC._characterState);
// you are receiving it as a CharacterState oject.
myC._characterState = (CharacterState)stream.ReceiveNext();
Try changing it to:
myC._characterState = (int)stream.ReceiveNext();