- Home /
Photon animation issue
Hi, I'm working on a multiplayer FPS and I'm using Photon+ for the networking. I have two scripts: one that does movement, and one that sends that movement to other clients. However, the movement is working fine, but the animations aren't. By the way, I'm using legacy animations, not animator.
Here's the one for movement
using UnityEngine;
using System.Collections;
public enum AniState
{
Idle,
Walking,
Jumping,
Firing
}
public class Movement : MonoBehaviour {
public float defaultSpeed = 10.0f;
public float jumpSpeed = 20.0f;
private Vector3 direction = Vector3.zero;
private CharacterController controller;
private Animation ani;
private float verticalVelocity = 0;
public AniState state;
// Use this for initialization
void Start () {
controller = GetComponent<CharacterController>();
ani = GetComponent<Animation>();
// ani.wrapMode = WrapMode.Loop;
}
// Update is called once per frame
void Update () {
direction = transform.rotation * new Vector3(Input.GetAxis("Horizontal"), 0, Input.GetAxis("Vertical"));
// CurrentAnimation();
if (direction.magnitude > 0.5f)
{
direction = direction.normalized;
state = AniState.Walking;
}
else
{
state = AniState.Idle;
}
/* if (Input.GetButton("Vertical")) {
ani.CrossFade("soldierWalk");
controller.SimpleMove(direction * defaultSpeed);
}
else {
ani.CrossFade("soldierIdle");
}*/
if (controller.isGrounded)
{
verticalVelocity = 0;
}
else
{
state = AniState.Jumping;
}
if (controller.isGrounded && Input.GetButtonDown("Jump"))
{
verticalVelocity = jumpSpeed;
}
CurrentAnimation();
}
void FixedUpdate()
{
Vector3 dist = direction * defaultSpeed * Time.deltaTime;
if (controller.isGrounded && verticalVelocity < 0)
{
state = AniState.Idle;
verticalVelocity = Physics.gravity.y * Time.deltaTime;
}
else
{
if (Mathf.Abs(verticalVelocity) > jumpSpeed * 0.50f)
{
state = AniState.Jumping;
}
verticalVelocity += Physics.gravity.y * Time.deltaTime;
}
dist.y = verticalVelocity * Time.deltaTime;
controller.Move(dist);
// ani.CrossFade("soldierWalk");
}
public void CurrentAnimation()
{
switch (state)
{
case AniState.Idle:
Idle();
break;
case AniState.Walking:
Walk();
break;
case AniState.Firing:
Fire();
break;
case AniState.Jumping:
Jump();
break;
}
}
private void Idle()
{
if (state == AniState.Idle)
{
ani.CrossFade("soldierIdle");
}
}
private void Walk()
{
if (state == AniState.Walking)
{
ani.CrossFade("soldierWalk");
}
}
private void Fire()
{
ani.CrossFade("soldierFiring");
}
private void Jump()
{
if (state == AniState.Jumping)
{
ani.CrossFade("soldierFalling");
}
}
}
And here's the one for network movement:
using UnityEngine;
using System.Collections;
public class NetworkMovement : Photon.MonoBehaviour {
private Vector3 realPos = Vector3.zero;
private Quaternion realRot = Quaternion.identity;
private Animation ani;
private bool gotFirstUpdate = false;
private AniState state;
// Use this for initialization
void Start () {
// ani = GetComponent<Animation>();
}
// 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((int)state);
stream.SendNext(transform.position);
stream.SendNext(transform.rotation);
Movement move = GetComponent<Movement>();
stream.SendNext((int)move.state);
}
else
{
// state = (Movement.AniState)(int)stream.ReceiveNext();
realPos = (Vector3)stream.ReceiveNext();
realRot = (Quaternion)stream.ReceiveNext();
Movement move = GetComponent<Movement>();
move.state = (AniState)stream.ReceiveNext();
if (gotFirstUpdate == false)
{
transform.position = realPos;
transform.rotation = realRot;
gotFirstUpdate = true;
}
}
}
}
If anyone knows a solution, please tell me. I know I'm not the first guy to ask this question, but I have been working on this for a few days. Any help would be much appreciated!
Your answer
Follow this Question
Related Questions
Photon Syncing Animations 2 Answers
Animation via Photon Networking without Animator 0 Answers
Enable logging in custom script (Photon Server) 2 Answers
Photon Instantiate doesn't always work 0 Answers
Photon Play Animation 1 Answer