Specify the playing animation

Hey everyone, I’m working on a 2D platformer right now,with the “new” 2D assets and everything. I just have a question, since I’m kind of new to Unity in general. How do I specify what animation that is currently playing on something through a script (Specifically C#, which I’m also new to, but it’s not that different from java so I’m just learning how to use the methods included with unity.)

My script:

using UnityEngine;
using System.Collections;

public class Mover2D : MonoBehaviour {
	void Start () {
	
	}

	void Update () {
		if(Input.GetKey(KeyCode.RightArrow)){
                       //Right. Here is where I want to specify the animation for walking to the right(Which itself is called WalkRight)
			transform.position =(new Vector3(1,0,0)/10)+transform.position;
		} else if(Input.GetKey(KeyCode.LeftArrow)){
                       //Left. Here is where I want to specify the animation for walking to the left(WalkLeft)
			transform.position =(new Vector3(-1,0,0)/10)+transform.position;
		}
                if(Input.GetKey(KeyCode.UpArrow)){
                        //"Fly" jumping.
			transform.position =(new Vector3(0,1,0)/2)+transform.position;
		}
	}
}

Thanks for the help. I literally wrote this up in a few seconds, but most of what I’m doing is in-game visuals,getting the terain,ect. It’d be nice to have these animations though.

So I need to use like tk2dSpriteAnimator or something? Because that’s not working. I would love some help on this.

Still need help.

And just in case, Unity - Manual: Animation Reference You may want to view this, and may also want to view (2D) Unity Animation video tutorials for anything complicated.

To answer the base question: You can either load several clips in the Inspector or use AddClip to add the respective clip to the Animation component, and then use Animation.Play to play that clip.

(Found with a bit of quick Googling ;))

I couldn’t find any of that for the life of me. >.< I suck at googling. It always gives me the same results. But thanks a ton!

So now when I add the Component “Animation” It will let me pick an animation, but in-script I still don’t know how to load an animation file itself.

I have each file. When I use animation.AddClip() It technically works. But the problem is that I don’t know how to specify which specific file to use. How do I “Load” the .anim file I made?

I’ve been looking around,and everyone else apparently just says animation.Play(“animationname”) works somehow. I put animation.Play(“WalkLeft”) And it just says that WalkLeft could not be found.

I haven’t touched multiple Animations in a while (my current project is a little light on them), but basically as far as I can tell via the docs and memory, you add the clips either in the Inspector OR via code (using Animation.AddClip) to add to the Animation’s components current selection of animation clips. You then use Animation.Play to CHOOSE which clip to play. However, if you don’t specify which one to use, it’ll use the default clip.

Also, you may want to look at the other Animation properties and methods, for things like transitioning from one animation to another and such. Oh, and I believe that there are quite a few tutorials out there to work with walking animations. If you’d like, I can possibly pull out a few from the endless stream of thy internet.

Edit: Here’s a 2D one (it’s a series as well):

And this guy covers a ton of things in his tutorials, and in this one, he does what you’re interested in as far as I can tell:

I really at this point just want to know how this works XD. I’ve dragged the .anim files onto the object that has the script. That made the editable when I have the object selected,so i’d think they would be attached to that object. Still nothing.

EDIT: Thing about that first one is that it’s not in-script. It’s just movement speed. Second one, it uses a whole different method of animating.

What do you mean you dragged the .anim files (going to assume that you properly made them and everything, and we’re not talking about sprites and I just realized those YouTube links I posted are about sprites)?

To make this simple…
1 Animation

  1. Create the animation in the appropriate way (inside or outside of Unity doesn’t matter, although Legacy/Generic type matters for this)
  2. Attach an Animation component to the object (typically imported objects with an animation once marked “Legacy” under “Rig” have this automatically)
  3. For the first field next to “Animation”, drag your clip onto that field if you’re clip isn’t there
  4. Use Animation.Play() in your code to play it

Multiple Animations

  1. Make sure you have all the animations PROPERLY made and such, let’s say “walk” and "idle
  2. Add the Animation component
  3. Under “Animations” change the array size to two
  4. Drag your 2 animations into the two fields (the default animation doesn’t matter too much). Pretty much have to drag from the Project view
  5. Play the appropriate Animation via code by doing “Animation.Play(“walk”);” or "Animation.Play(“idle);”

That should be it. To test if you made an animation “properly” and to make sure it works, I’d suggest just to drag it into the default field first, check “Play Automatically”, and check it out real quick. Also, if an animated object seems to disappear when you animate it, double click it to check out where it is. Most likely it teleported to where it’s parent object is- or if there’s none, probably to the world origin.

That’s a quick crash course in Animations in general. For walking animations, it’ll be a little more complicated to get it working perfectly as you put move input and such, but this should answer your basic question.

Edit: I have absolutely no idea why I kept assuming you wanted to animate walking and such…

I Didn’t realize you could set multiple animation by just increasing that number. I think that will work,thanks. Give me a moment to test it

I made the animations in unity,using the “Sprite Renderer”. (I add a key,specify which part of a sprite in specific that uses, then add another,until i have all the frames in one animation.)

EDIT: Still didn’t work. animation.Play(“WalkLeft”) Still says that it can’t be found, even though it’s in the animation component

Posting the exact error (and the exact line(s) of code) would be helpful. Also, did it work with Play Automatically? (Just to make sure at this point whether its a scripting problem alone or not)

I can specify which aniamtion to play in the component,yes. It seems like it’s something in the script… Here’s what I have:

using UnityEngine;
using System.Collections;

public class Mover2D : MonoBehaviour {
	double timer = 0;
	bool jumpable;
	
	// Use this for initialization
	void Start () {

	}
	void Update () {
		animation.Stop();
		if(Input.GetKey(KeyCode.RightArrow)){
			transform.position =(new Vector3(1,0,0)/10)+transform.position;
		} else if(Input.GetKey(KeyCode.LeftArrow)){
			transform.position =(new Vector3(-1,0,0)/10)+transform.position;
			animation.Play("WalkLeft");
		}
		if(Input.GetKey(KeyCode.UpArrow)){
			transform.position=(new Vector3(0,1,0)/10)+transform.position;
		}
	}
}

The error:

The animation state WalkLeft could not be played because it couldn't be found!
Please attach an animation clip with the name 'WalkLeft' or call this function only for existing animations.
UnityEngine.Animation:Play(String)
Mover2D:Update() (at Assets/Animations/Mover2D.cs:20)

It also says this:

The AnimationClip 'IdleLeft' used by the Animation component 'Object_0' must be marked as Legacy.
UnityEngine.Animation:Play(String)
Mover2D:Update() (at Assets/Animations/Mover2D.cs:20)

It says to attach it, but i don’t understand how. It seems to be able to find the IdleLeft animation… Which is interesting,because it’s the last animation on the animations list.

Sorry for not seeing that you replied.
For IdleLeft, that should be relatively easy. If it’s an imported object, change the animation type to legacy. If it’s a separate clip, click on the clip in the Inspector, change the mode to Debug (click on the icon next to the lock in the top right, click Debug from the drop down), and change the Animation Type to 1.

Now then, for the WalkLeft animation. Make sure you do it something like this on the object with this script:
1500137--84349--$AnimationEx.png
(The same animation is there 3 times simply because I expanded one of my current objects to demonstrate a point)

The green box is the default animation clip, and the blue box are the ATTACHED animation components. Honestly, the default animation component doesn’t matter too much- it just defines what animation.Play() will do (the one without specifying anything in the quotes). What you want to do is change the number in the size field (next to the white star) to 2 or however many animations you have. Then drag the animation clips into the boxes that are available (or click on the little circle to the right to pick from a list).

By the way, where does line 20 come in at all, in that second error? I don’t even see where “IdleLeft” comes from.

By the way… we’re not talking about Mecanim are we? They’re two pretty different systems…