lørdag den 2. januar 2016

Animation 'blending' in Unity

Work continues on my game-project, and today I arrived at an interesting issue which I hadn't been presented before - namely how to get two (or more) different animations to work together.
For example, a character walking while firing a bow, or jumping and swinging an axe.

This issue (and writing this post) brings to mind my bachelor-thesis on my previous education, where I created the graphics for the product - we had a soldier with a run animation and a shoot animation, but the animations were not blended in that project, so shooting and moving broke the animations/immersion.

For this project, what I needed was for my character to walk and spew a sort of breath-ability (essentially a shooting attack) at the same time. When a part of the GUI was clicked, the function would be called to start the animation, and the animation would at a same time trigger an Animation Even calling another function which would instantiate the particle effect.

The standard transitions from Unity did not do the job of blending the animations, so after some research online I found that a new animation layer must be created under the Animator component, and the 'priority' animation, which should cancel out part- or all- of another animation should be put on this layer, and the Weight of the layer should be set to one.
With this setup, the character would indeed walk and 'breathe' and the same time, and the animations would blend adequately, but the animation was looping continuously. This was disabled under the animation component, but the animation was still playing once at the beginning of the scene. As the animation State was on a layer of its own, the only transition was from Any State to "Breathing", with Breathing being the default state. To fix this final error I created an empty state and made that the layers default state, and the animations worked as they were supposed to.

This was a bit of a victory for me, because it opens up the opportunity for creating all sorts of animated stuff in Unity. I quite like the Mechanim-system, once you get used to it.

Here is a screenshot for the game. The frostbreath has not been added to the official release yet, but will be soon. The game is called Jingle Hells, the same game as I have mentioned before.

Ingen kommentarer:

Send en kommentar