I have been playing around with the Photon Network Engine recently, all in all it makes setting up a multiplayer environment very easy. However now that I want to start introducing it into my own projects I’m coming across some problems.
Here’s a little background about the Photon demo. The character prefab contains a third person control script, an animation element with animation clips, and an animation control script. It has other things such as the camera control, collider, etc but the first three are the main ones I’m struggling with. The control script controls the character movement, adding force forward and backward, then vertically for jumping. The animation control script invokes the relevant animation when the appropriate button is clicked.
With my current project I have chosen to use the Animator which handles the movement and animation all in one, it makes it nice and easy for me to stick any animation on my model . The problem I’m having now is getting the Animator running on its own instance of the character prefab. At the moment on each individual instance of the game, when I perform an action which invokes an animation, it animates all characters including the remote ones with that animation. What I cant seem to figure out is how to go about making an animator only invoke the animations on the appropriate character, and how to make it so that when a remote user performs an action, that action animates their character on my screen. Basically I’m having trouble syncing the animators to the appropriate character instances.
I sort of understand how it works with the local and remote users but I’m still struggling to get my head around this, any help will be much appreciated.
I think i get here a little late but i’m using a Animator component in my Photon Networking proyect, so i think i could help.
The prefab must have a photon script called PhotonView that is icluded in the Photon package and a script that you can found in the Viking demo from the asset store named “Third Person Network” (attached to the field “observe” of the PhotonView script). This last one (Third Person Network) is the one that send and recive the information from the Photon network, so i send the parameters of the animator component (in my case are a float “Speed” and a bool “isRunning”) and then recive them as it follows:
void OnPhotonSerializeView(PhotonStream stream, PhotonMessageInfo info)
{
if (stream.isWriting)
{
//We own this player: send the others our data
stream.SendNext(animator.GetBool("isRunning"));
stream.SendNext(animator.GetFloat("Speed"));
stream.SendNext(transform.position);
stream.SendNext(transform.rotation);
}
else
{
//Network player, receive data
isRunning = (bool)stream.ReceiveNext();
Speed = (float)stream.ReceiveNext();
correctPlayerPos = (Vector3)stream.ReceiveNext();
correctPlayerRot = (Quaternion)stream.ReceiveNext();
}
}
private bool isRunning;
private float Speed;
private Vector3 correctPlayerPos = Vector3.zero; //We lerp towards this
private Quaternion correctPlayerRot = Quaternion.identity; //We lerp towards this
void Update()
{
if (!photonView.isMine)
{
//Update remote player (smooth this, this looks good, at the cost of some accuracy)
transform.position = Vector3.Lerp(transform.position, correctPlayerPos, Time.deltaTime * 5);
transform.rotation = Quaternion.Lerp(transform.rotation, correctPlayerRot, Time.deltaTime * 5);
animator.SetBool("isRunning",isRunning);
animator.SetFloat("Speed",Speed);
}
}
Hope this is usefull