2
\$\begingroup\$

The way the program is supposed to run is basically, a character can be running/idle and if spacebar is pressed, all movement stops and movement controls are locked through the bool "canMove". The integer shockwave is a 1-4, directly correlating to the direction 1-4 (1 = north, 2 = east, 3 = south, 4 = west). Then, the animation plays and when it is over the controls are unlocked and you can move freely.

The problem I have encountered is, I can find no way to specifically tell when the animation is ending inside of a blend tree. I cannot find a way to allow the animation to play, and then continue to unlock the controls.

Since this site won't let me post more than three links, I will have to describe the animator window. Basically I have four idle states (N, E, S, W). Each motion is a blend tree, and each state is connected with a transition purely based off of int direction.

Blend Tree

This is where I tried adding another motion to the blend tree called shockwave, based on threshold speed (-1). This worked up until the point that I realized I would not be able to add anymore attacks or states, and that I could not tell when the animation had finished in order to unlock the controls.

Blend Tree Inspector

void Update (){

    if (Input.GetKey (KeyCode.Space)){

        canMove = false;
        direction = animator.GetInteger ("Direction");
        animator.SetFloat ("Speed", 0.0f);

        if (direction == 1){
            animator.SetInteger("Shockwave", 1);
            animator.SetFloat ("Speed", -1.0f);

            //find a way to detect that the attack animation is done.
            //continue and allow movement
            canMove = true;

                }
            }

        else if (direction == 2){
            animator.SetInteger ("Shockwave", 2);
            //if(!anim.IsPlaying ("character_shockwave_2")){
                //canMove = true;
            //}
        }
        else if (direction == 3){
            animator.SetInteger ("Shockwave", 3);
            //if(!anim.IsPlaying("character_shockwave_3")){
            //  canMove = true;
            //}
        }
        else if (direction == 4){
            animator.SetInteger ("Shockwave", 4);
            //if(!anim.IsPlaying("character_shockwave_4")){
                //canMove = true;
            //}
        }

Attempted solutions:

I have tried to use waitforseconds(x), and just wait for the attack animation to finish, and then after that amount of seconds I would set the canMove bool to true. For whatever reason, I cannot get this to work.

I have also tried to directly find if the animation is done playing through a few different ways, but I learned that the blend tree does not allow this.

The only solution I have thought might work is actually manually playing all animations through code alone. (Would require re writing the entire program).

If any further code is needed, I can supply it.

\$\endgroup\$
  • \$\begingroup\$ Have you tried using "OnStateExit" ? unity3d.com/learn/tutorials/modules/beginner/5-pre-order-beta/… \$\endgroup\$ – jgallant Dec 1 '15 at 21:49
  • \$\begingroup\$ "OnStateExit is called on the last frame of a transition to another state." Would this apply to transitions inside of a blend tree? \$\endgroup\$ – AspiringGenious Dec 2 '15 at 7:14
  • \$\begingroup\$ This would be a script that you attach to your Animator's State. So every single time that you would exit a state with this script attached, you could execute some code. I can add an example below, but it is pretty self-explanatory from the link I provided. \$\endgroup\$ – jgallant Dec 2 '15 at 11:02
  • \$\begingroup\$ Going to try and implement this in the next hour and a half. I'll let you know how it goes. \$\endgroup\$ – AspiringGenious Dec 3 '15 at 17:36
  • \$\begingroup\$ Would you look at that, it works. I added the BaseSMB script to the shockwave state using regular transitions between the attack and the idle state. Thanks for your help, if you can put it as an answer I will select it. \$\endgroup\$ – AspiringGenious Dec 3 '15 at 17:57
1
\$\begingroup\$

Have you tried hooking into the Unity OnStateExit?

https://unity3d.com/learn/tutorials/modules/beginner/5-pre-order-beta/state-machine-behaviours

This will allow your state machine to execute code every time you exit a state.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.