- Home /
Moving a child object during an animation (2D)
I have a 2D character that's set up with an idle animation but I need to be able to rotate a child object (his arm). I also need the rotation to stay in place when playing a walking animation too.
The idea is you can rotate the arm (it has a gun at the end) whilst standing still and also whilst walking. I don't need any transitions as such between the different animations but I need the gun rotation to stay the same.
The problem is that when playing any of the animations the arm cannot be rotated.
The best I can come up with is to set an empty game object on the character where the arm pivot should be then programatically have the arm move to that point.
I was wondering if anyone knows of a better way to do it?
Thanks
Answer by steakpinball · May 26, 2014 at 06:13 PM
I believe what you are looking for is Animation Layers. Animation layers allow you to override the base animation with another animation. They are commonly used to animate a specific body part without animating the entire body. In your case the base layer would have the idle and walking animation and another layer would have the animations for the arm rotating.
Official documentation https://docs.unity3d.com/Documentation/Manual/AnimationLayers.html
Official Training video https://www.youtube.com/watch?v=fi4O1i8a2Ro
An example in 2D from the live training http://youtu.be/uv2QSvuwIDs?t=35m19s
Thanks for the reply, that's very helpful. $$anonymous$$y issue though is that moving the arm up and down isn't an animation as such, it's done by user input (whilst the up arrow is held, rotate the gun upwards). With that in $$anonymous$$d the angle can vary.
I've managed to get it more or less working by using LastUpdate() and applying a new rotation to the arm every frame. This feels like a bit of a hack though.
Ideally I suppose I'd use animation layers and have an 'Aim' layer set to override that somehow takes an input/rotation.