- Home /
How to trigger animaton on two different Game Object's at the same time?
Scene and assets attached in zip file.
A cube and sphere start in a single frame default state, and jump/spacebar triggers a state transition to a simple looping animation on each. A game controller script takes input and calls a movement method in another script attached to both objects.
They work individually but they won't both trigger at the same time with one input. I can trigger one with input and then manually press the trigger in the Animator window to activate the other but of course that's not what I want.
Is there a way to get this working? Or a better way to handle this? Is there a way to make sure they stay 100% in sync? The actual context I need this for is a character and prop setup where each is it's own game object, animation clips, etc and must stay in sync. I 'could' just make the prop a part of the character but I figured a setup like this would make it easier to do things like apply physics to the prop if it is dropped by the character, for example.
Thanks for any help!
Answer by JACLEMGO · Oct 30, 2018 at 02:55 AM
The Observer pattern would be perfect here... http://gameprogrammingpatterns.com/observer.html
See attached your code with Observer pattern implement, much more scalable/testable/reusable.
[1]: /storage/temp/126883-kemijo-observerunitypackage.zip
Firstly thanks for this @JACLE$$anonymous$$GO, by answering my question you created several I hadn't asked yet, which are now clearer once I explored not only how it works but why I should use it. Very helpful.
However one thing I still don't understand is why $$anonymous$$e did not work the way it was. I added a line to change the material color to the move() method in my original scripts, and that worked fine on both objects but the animation was still only triggered on one. Could you or anyone else explain why? Hoping for a fuller understanding of why this doesn't work.
You had a Singleton reference to the Animator:
static Animator thisAnimator;
if you changed that to:
private Animator thisAnimator;
It would also work
And there it is, all makes sense now. Thanks again.