Transitioning between 2D animations by frame
I am having issues with Unity's default 2D animation system, and am curious as to how I can achieve a desired behavior. Within my project, I am attempting to transition between my character's "Run" animation and "Running Attack" animation to allow the character to smoothly attack while moving.
The way I've been looking at handling this is by creating 3 different animations- one normal run cycle, one version of the same cycle with the character drawing their weapon, and a third run cycle with the weapon drawn. This way when the player attacks, the sprite's run cycle would play the next frame of the "Draw Weapon" cycle, then the next frame would be from the "Weapon Drawn" cycle, before returning to the original cycle.
Here is an image from an existing game illustrating what I mean:
My issue is that I'm not sure exactly how to transition between animations on specific frames. I've considered using a blend tree, but I'm not quite sure it will achieve what I want.
Answer by Zoogyburger · Feb 25, 2016 at 06:08 PM
So you have a run animation and a running attack animation. Create two states in the animation controller with the two animations in them and have them transition to each other. Add a bool perimeter IsAttacking and one transition set the conditions to be IsAttacking to be true and the other to be IsAttacking to be false and add a script like this on to your player:
Animator anim;
private bool attacking;
void Start () {
anim = GetComponent<Animator>();
}
void Update (){
if (Input.GetKeyDown (KeyCode.Space))
{
anim.SetBool("IsAttacking", true);
}
Sorry, I guess I didn't explain the situation clearly.
If you attempt to transition between animations this way (which normally would work fine), it begins the next animation on the first frame of the next animation, rather than the next frame of that animation. Looking at the diagram I posted and using your method, If you were to hit Space on frame 5 of the first row, the animation would switch to the FIRST frame of the second animation, then play the entire second animation clip before playing the third clip.
What I want to do is interrupt on that 5th frame, then play the 6th frame of the second clip, the 7th frame of the third clip, and return to the 8th frame of the first clip.
Oops! I didn't understand what you were trying to do. I guess this sort of thing could be done with blend trees though I'm not exactly sure how. I would use my system above and in script, jump to the certain frame you want:
http://docs.unity3d.com/ScriptReference/AnimationState-time.html http://docs.unity3d.com/ScriptReference/Animator.html
Something like this:
GetComponent<Animator>()["Attack"].time = 2.45F;
Animator its the interface to control mecanim, not a animation dude.
Answer by J0hn4n · Mar 08, 2017 at 01:25 AM
weell i had a isuees like that before, i recomend you to use bleend trees only to make things like movement, like if speed its less that 0.5 walk, else run.
its better use "Animation" for attack or things you need to know if has finished, you can set animation.playqued if you want make attack combos ,also you can get the normalized of that animation to do another stuff, like send a damage event when the normalized time reach to 1.
Also you can always make a list of sprites and make your own custom animation.