- Home /
Why is my char animation not showing on my opponent's Photon window?
Currently doing a small test game using Photon PUN2. When moving, my character moves into an animation state where it leans forward 12 degrees on the x axis. Each state (standing still and moving) is a single frame, with the currently played state determined by a boolean named 'Moving'.
When testing this (https://reverend-speed.itch.io/photon-test-01 pw: photon), I can see that while my character 'animates' correctly on my screen, but it does not animate on my opponent's screen (and vice versa for my opponent).
Presently I have a Photon Animator View on the parent of the character model, tracked by a Photon View on the root gameobject parent of the character.
Why is my character animation not showing up on my opponent's screen?
My character code is as follows:
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.InputSystem;
using Photon.Pun;
using Photon.Realtime;
public class CharMotor : MonoBehaviourPunCallbacks
{
#region Private Variables
private Controls inputActions;
private Rigidbody rb;
private Vector2 inputMoveXZRaw;
private Animator anim;
private PhotonView view;
private Text textScore01;
private Text textScore02;
#endregion
#region Public Variables
public float speed = 8.0f;
public int playerNumber;
#endregion
private void Awake()
{
inputActions = new Controls();
rb = GetComponent<Rigidbody>();
view = GetComponent<PhotonView>();
textScore01 = GameObject.Find("TextScoreP01").GetComponent<Text>();
textScore02 = GameObject.Find("TextScoreP02").GetComponent<Text>();
}
public override void OnEnable()
{
inputActions.Enable();
inputActions.ActionMap.MoveXZ.performed += HandleInput;
inputActions.ActionMap.MoveXZ.canceled += HandleInput;
GetComponentInChildren<Text>().text = view.ViewID.ToString();
}
void Start(){anim = GetComponentInChildren<Animator>();}
private void FixedUpdate()
{
if (Mathf.Abs(inputMoveXZRaw.magnitude) > 0.01f)
{
Vector3 desiredDirection = new Vector3(inputMoveXZRaw.x, 0.0f, inputMoveXZRaw.y);
rb.MovePosition(rb.position + desiredDirection * Time.deltaTime * speed);
rb.MoveRotation(Quaternion.LookRotation(desiredDirection));
anim.SetBool(anH.moving, true);
}
else
{
anim.SetBool(anH.moving, false);
}
}
public override void OnDisable()
{
inputActions.ActionMap.MoveXZ.performed -= HandleInput;
inputActions.ActionMap.MoveXZ.canceled -= HandleInput;
inputActions.Disable();
}
private void HandleInput(InputAction.CallbackContext ctx)
{
if (view.IsMine) inputMoveXZRaw = ctx.ReadValue<Vector2>();
}
private void OnCollisionEnter(Collision collision)
{
if (collision.transform.CompareTag("Bullet"))
{
photonView.RPC("IncreaseScore", RpcTarget.AllBuffered);
}
}
[PunRPC]
private void IncreaseScore()
{
if (view.IsMine)
{
textScore01.text = (int.Parse(textScore01.text) + 1).ToString();// Convert string to int, add one, and convert back.
}
else
{
textScore02.text = (int.Parse(textScore02.text) + 1).ToString();// Convert string to int, add one, and convert back.
}
}
public static class anH
{
public static int moving = Animator.StringToHash("Moving");
}
}
Answer by Reverend-Speed · Jan 21 at 05:02 PM
The code within FixedUpdate needed to be wrapped within a PhotonView.IsMine check - after which, everything worked smoothly. Credit to @Meep on the Photon Discord for the solution.