How To Combine Two Animations In Unity
How to run two animations at once? This is 1 of the nigh common trouble in the animations field. Permit'due south meet how Unity tin help u.s.a. to solve this problem.
Permit's assume this situations: Y'all have a 2nd character that has a run animation and a run-assail animation, and yous want to make the character be able to run and attack at the same time. Now the simple solution is to play the run animation when when actor outset to run, and switch to the run-attack animation when the player press the attack push button, the problem is that the transition betwixt the run and the run-attack blitheness is non a shine transition. The problem can be solved with 2 solutions:
Animation Blend
The easy one is to apply the Blitheness.Blend method provided by unity:
public void Blend(string animation, bladder targetWeight = one.0F, float fadeLength = 0.3F);
What does this method do?
Information technology Blends the animation named animation towards targetWeight over the next time seconds. Basically is a smoothen way to get from one animation to another one.
Run two blitheness at one time.
But what if you accept ii very complex animations which are very difficult to synchronize? the best solution is split up the 2 animations into three animations:
- Ane is the animation used to animate the legs.
- Another is the animation used to animate the torso.
- Another is the animation used to animate the torso while the attack is occurring
One time we have the animations, when the player press the push button to assail while the character is running, only the the body animation volition be switched, and to make the transition fifty-fifty more than smooth, we'll utilise the Animation.Alloy method to change from the run animation of the body to the run-attack animation of the torso.
Let see how to utilise Unity to attain the issue that we want:
First affair to do Is to create the Avatar to apply. Let's create a Hero grapheme with a rigidbody attached:
Has yous tin see, our hero has one child gameObject named Body where we'll use to attach the animations of the body, call back to attach a sprite rendere to the body if your blitheness is sprite based:
Now Import your animations sheet:
As you can meet, we have iii animation, ane for the legs and two for the body..
Create the animations:
Now permit's add the animations to our character:
Select the legs and drag the frames into the actor gameobject to automatically create an blitheness clip:
Once done, an animation controller along with an animation clip will have been created:
Now create the animations for the body, open the animation windows –> create new clip and select the body gameobject sprite rendere component:
At present add together the animations frame:
Do the same affair for all the animations that needs to be played on the upper part of the character:
Setting the animator:
Now open up the animator windows and create 2 states: Idle and run and a trigger that will switch from the Idle to the run state:
If we trigger the Playr_run condition, our blitheness will become from the Idel state to the Run country, merely this will but show the lower office of the animation, now nosotros accept to add another Layer to our animator, the new layer will allow usa to add some other blitheness that volition run simultaneously with the legs blitheness.
Go to Layer nether the animator window and click on the plus button to add a new layer:
Click on the gears icon and set is as shown in the following film:
- The weight is the priority of the animations A higher layer number has a higher priority.
- The sync property volition create an copy of our outset layer of the animator but with the advantages that on this layer, we can choose to run different animation. Go to the run animation on the layer that we've just created and gear up the animation clip of the animation to the 1 relative to the upper part of the body:
We have just create a animator with ii layers that will practice exactly the same, but on 1 layer nosotros'll run the legs blitheness, and on the other the body blitheness.
Now the just thing that you lot have to do, is to trigger the animations in code:
Here I gave y'all the idea of how to run two animations at once.
This base of operations example can be easily expandable to run different animations or even more and so 2 animations at one time.
Source: http://gamedevelopertips.com/unity-run-2-animations/
Posted by: hurstdreir1946.blogspot.com
0 Response to "How To Combine Two Animations In Unity"
Post a Comment