- Home /
how to make game object animation without animator visible to all players in photon pun?
hello, sorry if the title is a little confusing. i am using photon pun 2 with unity and am making a multiplayer game. I have a gameobject with the animation component on it. i have a script on that object that plays the animation when any key is pressed. (Script below) this script works fine for the player that this object is a child to, that player can see the animation. however, for any other player the "Monster" game object sits still and does not play the animation. i know i need to sync up the animations somehow but when i looked it up all the fixes i could find involved the animator component and the photon animator vew script. is there a way to play the animation in my script like below, and have the animation visible to every player, not just the player the object is a child to? :D (the script is called MonsterWalk)
Script:
[SerializeField]
private GameObject Monster;
void Start()
{
}
void Update()
{
if (Input.anyKey)
{
Monster.GetComponent<Animation>().Play("Walk");
}
}
game object with script attached:
game object with animation attached:
Answer by Captain_Pineapple · Apr 09 at 09:53 AM
Hey there,
So the straight forward way to solve this would be to introduce an RPC. See here for documentation This basically enables you to call a function that is executed on all Clients. So in this case you could create a RPC called startAnimation
that you execute when a button is pressed. Then the function just has to trigger the Animation on the object and thus that status is somewhat synced.
Given that you want to sync Animations i would not recommend to do this like i described above though. This will only lead to issues even though it is for now the easiest solution.
A way to solve this better assuming that you will add Movement to your monster:
As the animation you want to trigger is the walk animation i'd suggest that you create some logic on your MonsterWalk
that locally ties the currently executed Animation to the Speed of the Monster. So:
//assuming that you have 2 animations "Idle" and "Walk" that transition between each other if "Walking" is true/false:
if(speed > someValue)
animator.SetBool("Walking", true);
else
animator.SetBool("Walking", false);
If you have this in place you do not have to sync the animation explicitly -> instead you sync input to steer the monster which implicitly will trigger the correct animation as soon as the monster moves.
In multiplayer games you don't have to explicitly sync everything, you can get away with faking a lot of things and this is something that you have to do in some way as sending all information that makes up the current state of your scene is often not possible.
let me know if something was unclear then i can try to elaborate a bit further on this.
Answer by WerewoofPrime · Apr 12 at 12:42 AM
hello CaptianPineapple! thank you for your well-explained and detailed answer! not only did you answer my question with a PERFECT and WORKING solution but you solved many of my other problems as well! thank you! :D this is exactly what I need.