change animation direction

Hello,

this is probably simple for all the pros out there… yes i am a noob :frowning:

anyways, 2 things actually, 1. i am trying to change the direction depending on the input of the player, if right (either arrow right, letter D etc.), then use this animation because i have an animation for right. if left arrow or A etc., turn direction left.

i do have this code below:

        if (Input.GetKeyDown(KeyCode.D))
        {
            animator.SetBool("isLeft", false);
            Debug.Log("turn right");
        }
        if (Input.GetKeyDown(KeyCode.A))

        {
            animator.SetBool("isLeft", true);
            Debug.Log("turn left");
        }

and this works great, except, its only for Keyboard A, i want it to work for of course any other input, whether it be for android, xbox etc.,

however, when i try and change it to this:

        if (Input.GetKeyDown("left"))
        {
            animator.SetBool("isLeft", false);
            Debug.Log("turn right");
        }
        if (Input.GetKeyDown("right"))

        {
            animator.SetBool("isLeft", true);
            Debug.Log("turn left");
        }

this doesnt work, am i missing something? no error messages, i dont even see in the debug console screen so i am doing something simple and wrong… but what?

also, my animation to the right seems slower than the left… is there something i can check for in either the code or the animation properties?

thanks in advance :slight_smile:

Getting input is a complex study, with lots to consider. Too much, in fact, for a post here to contain.


That said, for GetKeyDown you should avoid the string names for input. They’re slower. Use the keycodes found in the documentation here. I believe “left” should be “leftarrow”, or something like that (I don’t use the strings much to remember), which is likely why that version doesn’t work.


If you’re interested in a more advanced approach that offers certain benefits, you might investigate EventType.KeyDown, which of course will have you searching and studying related concepts. One of the issues with GetKeyDown is the fact that it is queued by Unity for use when Update fires, and that means you have to check for each potential key of interest. EventType.KeyDown can be used to fashion a method that determines what key is pressed, from which you can take action. It is the reverse perspective, and very useful (but it is a bit more complex to implement).


You may also be interested in the documentation on Input.GetAxis, as that does ‘mutate’ based on platform, though it may not support the specific user interface concepts you prefer.


To take control of input, you have to consider the platform. It doesn’t make too much sense to try to fashion one ‘uber method’ that fits all platforms, because you’ll generally end up checking for keys on a mobile device that will never be used, and on other platforms you’ll be checking for input from sources that don’t exist.


You should consider searching for the C# subject “delegates”, and learn how to use them. At the risk of oversimplifying, a delegate represents a method using a variable. Your code can call the delegate, so that part of your code does not change. However, the delegate can be assigned to any matching method, which means you can configure the delegate based on which platform your application is running on. In this way you’d fashion a method specifically for the PC, another for mobile, another for consoles, as you see fit. At program initialization you inquire to determine what platform the application is on, and configure the delegate to point to the appropriate method for the type of input you require. This way you can support all forms of input for each platform, but avoid wasting time and over complicating code trying to force all of that to fit into just one sprawling bunch of code.


awesome big thanks @JVerne for your input, definitely now understand that i was doing something right :slight_smile: but now you open a new door for me to explore and big thanks to you on that, i will definitely want to study that as writing good code so i wont have to keep repeating the same thing over and over :slight_smile:

that being said, just 1 more question if you or anyone else doesnt mind, GetButtonDown vs Input.GetAxis, which would you prefer, if its a performance, or maybe its really a style preference?