Doubt of how to capture the animation of the character in the Unity

2

I'm trying to make a command that activates an animation when I press 2 keyboard keys and when I stop pressing one of them, that animation stops. For example I chose the "w" + "leftShift" key, when both are pressed I want to start the animation, "RUN". When I stop pressing "leftShift", I want it to start a walking animation "WALK", and lastly when I stop pressing "w", I want it to start an iddle "iddle" animation. can anybody help me? Here is the command I'm trying to make work:

        if (Input.GetKey(KeyCode.LeftShift) + (Input.GetKey ("w"))) {
            animationController.PlayAnimation (AnimationStates.RUN);
        } 
    
asked by anonymous 30.10.2018 / 16:56

3 answers

1

Use the && operator instead of + , if only W runs the walk animation. And use the GetKeyDown() method 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

    
30.10.2018 / 17:27
1

You can do it using if inside another:

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

Or you can do with 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 can not add booleans, true + true does not exist.

    
30.10.2018 / 17:32
1

This code might help you:

if (Input.GetKeyDown(KeyCode.LeftShift) && Input.GetKey("w"))
   {
     animationController.PlayAnimation(AnimationStates.RUN);
     if (Input.GetKeyUp(KeyCode.w))
        {
          animationController.PlayAnimation(AnimationStates.WALK);
        }
   }
    
30.10.2018 / 18:21