- Home /
Best Way - Animation - Multiple GameObjects.
Hello there, Happy New Year and best wishes for this one. I'll go straight to the point.
So, I want to make a GameObject Move. This gameObject will be My Player. I want this player to have some clothes. Shirt and a Pants.
hence I attached 2 GameObjects to the player.Shirt and Pants.
For this, I made One Sprite of a "naked" Player(and an actual 4 frame animation). Which I also made so for the Shirt and the Legs. The idea is that the 3 animations move when I press the Direction Button(Right arrow in this case).(GUI Repeat Button).
It works wonders if I press it once...But if a player double clicks the same button to move the 3 animations get unsynchronized and do something like this: Video of Bug on WorkSpace
Now, the animation has 2 states. Idle and Moving and its pretty much the same animatorController for the 3 gameObjects. I do have something like this, the actual code tells the gameObject and the children(Shirt and Legs are attached to Player) to move but the idea is the same.
FixedUpdate(){
if(_buttonisPressed)
PlayAnimation();
CheckIfButtonisPressed();
}
PlayAnimation(){
//Tells the Animator to run the Move Animation.
_attachedAnimatorComponent.SetBool(isIdle,False);
}
OnGUI(){
if(GUI.RepeatButton(new Rect(Screen.width - (75 * 2) - 10, Screen.height - 75, 65, 65),"Move"){
_buttonIsPressed = true;
} else {
_buttonIsPressed = false;
}
}
CheckIfButtonisPressed(){
if(_buttonIsPressed == true){
PlayAnimation;
}else {
_attachedAnimatorComponent.SetBool(isIdle,True);
}
}
So, why does the animation Glitch out? Is there any way that I can tell the animations to synchronize?
Try making an empty GameObject and making your three objects children of that object. Then, attach an animator controller to your parent GameObject. This will you allow to control all three GameObjects in one animation, keeping them from getting out of sync.
How would you call the three animations? Sorry, I added a gameObject, made it parent of the body,shirt and legs.
Created a script called AnimatorController, attached it to the parent. Here it is:
public class AnimatorController: $$anonymous$$onoBehaviour{
private void Set$$anonymous$$oving(bool moves){
if(_bodyController != null)
_bodyController.Set$$anonymous$$oving(moves);
if(_shirtController != null)
_shirtController.Set$$anonymous$$oving(moves);
if(_legsController != null)
_legsController.Set$$anonymous$$oving(moves);
}
}
This calls the animator on each gObject(body,shirt and legs) each time the button is pressed but if the player clicks the button fast enough the animations keep getting out of synch. What am I doing wrong?
Thanks for your time!!
Sorry to reply to you in a different thread. Happy new year and thanks for your help. So, I've updated my question to a more 'formal', myWork kind of related.
I've been trying various ways to solve this problem but no one has given me an actual solution....Im getting desperate and I don't understand why this would happen...
Ins$$anonymous$$d of having three animator controllers on each game object just have one on the parent. Then when you create a new animation in this animator controller you can animate each game object in a single animation.
Thank you for your time. So,in this case, _shirtController is just a script that tells the AnimatorController which gameObject to animate.
_shirt(legs and body) have only 2 components attached. Controller script which sends itself as a gameObject just to know WHICH game object to animate and ofcourse the animator.
What line of code would you use to animate all 3 without having each controller? Sorry for being such a noob.... and again thanks for your time!
Your answer
Follow this Question
Related Questions
2D Animation does not start 1 Answer
Animator - Stoping && Playing from Specific frames. 5 Answers
Damn, HOW CAN I KNOW, IF MY 2d ANIMATION SPRITE IS FINISHED??!! 0 Answers
Object's sprite and collider are not at same position as transform 1 Answer
How to optimize 2D Animations created with 2D Animation packege? (v3.1.1) 0 Answers