Hi - I'm trying to animate a 2D character so that it has a continuous idle animation in his body, with a head that does a separate idle animation and a talking animation. Basically, he keeps moving his arms no matter what else happens. I'm trying with Sprites Unity as that seemed to be sufficient for what I need to achieve.
I've read the tutorial and manual, and downloaded the 2D head turning tutorial prefab, but I just cannot get it to work on my own character. I can get the body animating, using the normal process for animating a character, but I can't work out how to animate the head layer separately.
The manual says "If Head on separate layer? is checked, then an additional Head layer index field will appear and head animations (idle and talk) can be moved to a separate layer in the Animator component" - although I was able to create a separate layer in the Animator component, I wasn't able to move any animations to it or create any specific ones within it.
The prefab download readme says "The “Sprite child” is assigned as the Body sprite, and the Head sprite is then an immediate child of that", so I've created my NPC with the body sprite as a child and the head sprite as a child of that, so that's no problem. Then it says "The Animator has two layers...The Head layer, which has the head’s idle and talking animations" - I can see very clearly in the prefab how it's set up, with two separate animation layers, but I just cannot get that second layer working myself, and I don't know what I'm missing.
Any help would be really appreciated, thank you!
It looks like you're new here. If you want to get involved, click one of these buttons!
Comments
As in, the Animator's Head layer is empty? That'll be where head animations are played from.
These animations will need to be new ones, as they'll need to animate the head object only - not involve the body sprite. After creating an animation, you should just be able to drag it from the Project window into your Animator window with the Head layer selected. Is that not the case?
Thanks - this helped, I've now been able to create two separate idle animations, one for the body and one for the head, and put them into two separate animation layers. However, when I run the game, the body animation plays instead of the head (so there's the regular body animation, then where the head should be, the same body animation is also playing). I can't see why...
I have the Head Layer set to 2 in the NPC settings, and in the Sprite Renderer for each sprite I've set the Order In Layer to 1 for the body and 2 for the head. In the Animator layers, the body weight is set to 1 and the head weight is set to 0 (I didn't change those numbers).
The Head Layer in the NPC Settings refers to the Animator layer - not the Sprite Renderer's "Order in Layer" value. If your Animator has 2 layers - Base and Head - then set the "Head Layer" field to 1.
Check that the correct idle animation is assigned in the Animator's Head layer.
Thanks - I think that makes sense. I'll give that a try.
Thanks for being so responsive, by the way. It's fantastic, very much appreciated.
I'm still struggling - as far as I can tell I've got everything set up OK (equivalent to the example Brain animation) but I'm still getting duplicated animations.
Here's how things look...
This shows the body animation. I've called the animation clip Idle_D:
This shows the body layer within the animator. I've created a state called Idle_D, and I put the Idle_D animation clip into it as its motion:
This shows the head animation. I called the clip HeadIdle_D:
This shows the head layer within the animator. I created a state called Idle_D, and I put the HeadIdle_D clip into it. I also moved the weight slider to 1.0:
Finally, here's the NPC settings:
...and this is what happens:
I made sure to set the character's direction to face down on Start/Load in the scene, so I don't think it's an issue with the "_D" direction thing in the animation.
Does anything stand out as obviously wrong? Thanks again in advance for the help.
Does your character have a "Sprite child"? It'll need assigning in the NPC Inspector. Nothing stands out otherwise.
I'm confused, though: you said earlier that it was the body animation that was being duplicated - did it change to the head, or am I misunderstanding?
How many Animator components are on the NPC's object (+ children), and are there any other Animation States in the Animator that weren't shown in the screenshots?
Pause the game at the time the issue occurs, view the Inspector of object with the Animator component and open up the Animator window - what does it show is playing?
The body was duplicating previously, but when I change the weight of the Head layer in the Animator to 1.0 it duplicates the head instead. I set the Head layer back to 0, so now the body duplicates.
There is only one Animation State in each layer, both are Idle_D.
I kind of cracked it...but not quite. Progress, at least, maybe?
I assigned a sprite child to the NPC inspector on the parent object, and I put an Animator on the parent and child objects and assigned both to the same Animator Controller, then I ran the game and took your advice and inspected it.
When I run the game, the body duplicates. In the Animator window both animations are playing, but the Sprite Renderer shows that each object is cycling through the Body sprites. However, if I select the Head child object, go into the Animator while the game is running and slide the Head layer weight to 1.0, the Body and Head animate separately. Great!
So - for the parent object, the Body, the Animator has a Body layer with weight 1.0 and a Head layer with weight 0.0; then for the child object, the Head, the Animator has a Body layer with weight 1.0 (I can't move that slider from 1.0) and a Head layer with weight 1.0.
However, when I stop the game and try to change these sliders, sliding the Head layer weight changes the layer for both the parent and child objects - so if I select the child object, the Head, and slide the Head Animator layer to 1.0, when I select the parent object, the Body, the Head Animator has also been slide to 1.0...but if I slide it down to 0, then it also slides it to 0 in the child object Animator. Does that make sense?
Basically while the game is running I'm able to adjust only the Head layer for the child object, but outside of the game that Head layer adjust for both the child and parent objects. The layer weight seems to be what's getting in the way, but I can't seem to adjust it outside of the game running.
Don't rely on multiple Animator components - a single Animator, with multiple layers, can animate multiple objects/sprites at once.
Bear in mind that your placement of the Animator component affects the data in which animations are recorded. You'd need to ensure that both your body and head animations are created with the same Animator - on the same object - so that it can be properly played back at runtime.
That's what I thought - I connected the Animator component to the child object to test it and left it in because it didn't seem to affect things. I've taken it out.
Based on your advice about placement of the Animator component, I've deleted and redone the whole lot just to make sure I'm doing it OK - here's the steps I went through:
Create Animator Controller, call it "Carl", select parent (Carl, body) object, add Animator Controller component, associate it with Carl Animator Controller
Select Carl parent object, select Animation window, click on Create New Clip, create new clip called "CarlIdle_D", add sprites
Select Animator window, rename state from "CarlIdle_D" to "Idle_D" (I assume this is necessary as the NPC component has "Idle_D" as the expected name)
Rename layer to "Body", create new layer, rename to "Head"
Select parent object, select Animation window, select Create New Clip dropdown (note - it didn't seem to matter whether I selected the parent or child object when I did this), create new clip called "CarlHeadIdle_D", add sprites
Select Animator window, select Body layer, delete CarlHeadIdle_D animation clip (it put the clip in the Body layer, I couldn't get it to go straight into the Head layer or work out how to move it from one layer to another)
Select Head layer, drag and drop in CarlHeadIdle_D animation clip, rename to "Idle_D"
...and that's it. I have a single Animator Controller with two layers, and in each layer there's a different "Idle_D" clip. As far as I can tell that should work...? But I'm getting the same issue, so I assume I must be doing something wrong along the way.
When I run the game, both clips seem to be running - the blue bar runs left to right on each state in the Animator window - but while the Sprite Renderer for the parent (Body) object cycles through all the sprites in the animation, the Sprite Renderer for the child (Head) object is static with the first one.
The only thing that seems to affect it is the layer weight slider, during the game:
Body 1, Head 0 = Body animates as intended, Head is static
Body 1, Head 0.1-0.5 = Body is static, Head is static
Body 1, Head 0.51 or higher = Body is replaced by Head, which animates; Head is static
I think at this point the best way forward is for me to see it for myself. If you can put the shared assets (sprites, prefab, animation, animator controller) into their own folder, create a .unitypackage of it and PM it to me.
Idle_D is only expected by default - you can modify this field if you wish.