How to script Multiple animations to play at the same time?

I’m an animator. So take it easy… This is my first week using Unity and I’ve been stuck for a few days now.

What I’m trying to do is…

Have a script that is called when the user presses down a key. I want that script to start animations(made inside unity and attached to the game objects)

On different gameobjects obviously…

I attached the script to the main camera. If someone can show me what I’m doing wrong, I would greatly appreciate it. I have been scouring the Script Reference and tutorials to find an example but I can’t seem to find anything on this.

var sweep1 = GameObject;
var sweep2 = GameObject;
var sweep3 = GameObject;

function Update () {

if (Input.GetButtonDown (“1”))

var sweep1 = animation.Play (“smallsweep”);

var sweep2 = animation.Play (“bigsweep”);

var sweep3 = animation.Play (“giantsweep”);

}

Heya! I’m an animator too so I know how it goes! Unity’s pretty easy to use once you get your head around it though.

That said, you have got a few things wrong with your script, I’ll try walk you through them :

var sweep1 = GameObject;

This is trying to create a variable called sweep1 and assign GameObject to it, which doesn’t make much sense. What you want is

var sweep1 : GameObject;

Which says sweep1 is of type GameObject. Since you define it at the top of your script, you can fill it in via the inspector.

if (Input.GetButtonDown (“1”))

GetButtonDown uses axis you set up in the input manager, so unless you have defined an axis called 1, this won’t work. If you’re expecting it to fire when you press 1 on the keyboard, use GetKeyDown instead. You can read more about inputs in the docs. Also, because you left off the { }'s, this If statement will only apply to the first animation call, the other two would fire constantly.

var sweep1 = animation.Play (“smallsweep”);

var is used to define variables, since you’ve already defined it at the top, you don’t need to do it again. Also, you’re not assigning the animation event, you’re accessing it, so it should look like this

sweep1.animation.Play (“smallsweep”);

Basically you’re saying you want to take sweep1, access it’s animation component, and access the animation’s component Play function to play an animation called “smallsweep”.

So with corrections, you should end up with a script like this :

var sweep1 : GameObject; 
var sweep2 : GameObject; 
var sweep3 : GameObject; 

function Update () { 

if (Input.GetKeyDown ("1"))
{
sweep1.animation.Play ("smallsweep"); 

sweep2.animation.Play ("bigsweep"); 

sweep3.animation.Play ("giantsweep"); 
}

}

hope that helps!

1 Like

Sorry to dig this one up Sycle but I’m having trouble with animations at the moment. For the example code above, will the animations play in sequence? Is it possible to play it all at once? For instance if I have a jumping animation and a separate facial animation, how would I sync the facial animation to only play during the jumping animation?

Hope you can help.

Thanks very much.

  • Hakimo