Doubt how to capture the character’s animation in Unity

Asked

Viewed 159 times

2

I’m trying to make a command that activates an animation when I press 2 keys of Keyboard and when I stop pressing one of them, this animation stops. For example I chose the "w" + "leftShift" key, when both are pressed I want you to start the animation, "RUN". When I stop pressing "leftShift", I want it to start an animation of walking "WALK", and last when I stop pressing "w", I want it to start an animation of stopped "IDDLE". Could someone help me? Here’s the command I’m trying to make work:

        if (Input.GetKey(KeyCode.LeftShift) + (Input.GetKey ("w"))) {
            animationController.PlayAnimation (AnimationStates.RUN);
        } 

3 answers

1

Free up the operator && instead of +, if it is only the W rotate the walk animation. And use the method GetKeyDown() to identify a key pressed

if (Input.GetKeyDown(KeyCode.LeftShift) && Input.GetKey("w"))
{
    animationController.PlayAnimation(AnimationStates.RUN);
}
else if (Input.GetKey("w"))
{
    animationController.PlayAnimation(AnimationStates.WALK);
}

Unity - Scripting API: Keycode

  • Because I tested this method worked out the only problem that is happening and when I drop the "shift" but keep pressing the "w", it keeps doing the animation of running, only when I drop the "w" and tighten again that it makes the animation of walking.

  • 1

    That stretch is inside the Update()?

1

You can do it using a if within another:

if (Input.GetKey(KeyCode.LeftShift){
    if((Input.GetKey ("w"))){
        animationController.PlayAnimation (AnimationStates.RUN);
    }
} 

or you can do with the and using $$

if (Input.GetKey(KeyCode.LeftShift) && (Input.GetKey ("w"))) {
   animationController.PlayAnimation (AnimationStates.RUN);
}

The function Input.GetKey returns a Boolean you have to use logical operators for what you want to get, and you cannot add booleans, true + true there is no.

  • Because I tested this method worked out the only problem that is happening and when I drop the "shift" but keep pressing the "w", it keeps doing the animation of running, only when I drop the "w" and tighten again that it makes the animation of walking.

  • That’s usually how games work but if you want to change that is making a case to change the animation if !(Input.GetKey(KeyCode.LeftShift) && (Input.GetKey ("w"))) then stick what you want inside.

1

This code may help you:

if (Input.GetKeyDown(KeyCode.LeftShift) && Input.GetKey("w"))
   {
     animationController.PlayAnimation(AnimationStates.RUN);
     if (Input.GetKeyUp(KeyCode.w))
        {
          animationController.PlayAnimation(AnimationStates.WALK);
        }
   }
  • thanks for the help, but from the beginning it was not the commands that were more yes there in the Animator because a condition there was reversed. I solved the problem, but thank you for your attention

  • Glad it all worked out!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.