Search Unity

  1. Unity 2018.3 is now released.
    Dismiss Notice
  2. The Unity Pro & Visual Studio Professional Bundle gives you the tools you need to develop faster & collaborate more efficiently. Learn more.
    Dismiss Notice
  3. We've updated our Terms of Service. Please read our blog post from Unity CTO and Co-Founder Joachim Ante here
    Dismiss Notice
  4. Want to provide direct feedback to the Unity team? Join the Unity Advisory Panel.
    Dismiss Notice
  5. Improve your Unity skills with a certified instructor in a private, interactive classroom. Watch the overview now.
    Dismiss Notice

Make 2D character face direction of last pressed key?

Discussion in 'Animation' started by Denisowator, Jan 9, 2019.

  1. Denisowator

    Denisowator

    Joined:
    Apr 22, 2014
    Posts:
    860
    I'm doing a little animation thing in Unity to get my brain back into working with the Animator, and I'm following this tutorial.

    In the tutorial, the end result is 8 directional movement (with 4 directional rotation), and the player's facing direction changes once only one key is being held down.

    E.g.: If walking diagonally down-left and down was the first direction, the player will face down, until you lift the down key, in which case it will then face left. If this doesn't make sense, the guy showcases it at the end of the video.

    What I need is for the correct animation to play based on whichever direction was inputted the latest.

    E.g: If I'm going down, I'm facing down. If I then also hold down left, to go down-left, I want the player to face left

    There's a whole ton of settings and stuff in the animator. Most transitions have two conditions (for the horizontal and vertical movement), and there's all the Settings for each transition. So if you require more information, please let me know which information. Right now my setup is exactly the same as in that tutorial.


    P.S. The tutorial uses "transform.Translate" to move their player, which gives sharp movement. The transition conditions check for the direction of x and y (which are set in the player movement script). Since the movement is sharp, whenever the input changes, the player turns to face the new direction instantly.

    I'm using "rigidBody.MovePosition" so the movement is smooth. Could anyone suggest a different way of checking for movement? I'm not sure how to make it check for button presses directly (off the top of my head at least). Right now the Animator will wait for my character's x or y to set to 0 before it turns the player, so there's a delay.

    P.P.S Basically what I'm asking for is transition logic between the different animations (facing left, facing right, etc.), in terms of the various settings in the Animator window. Like what kind of transition conditions and properties would I have to set, to make the player turn the second I press a corresponding key (no matter where they're going)?
     
    Last edited: Jan 10, 2019
  2. Denisowator

    Denisowator

    Joined:
    Apr 22, 2014
    Posts:
    860
    Okay so I got my one working. I made a boolean for each direction, and in my script instead of setting x and y to the player movement, I toggle the boolean based on whether the corresponding key is pressed.

    I'm using GetKeyDown, which makes the boolean act (at least visually in the Animator window) as a trigger. Though with a trigger it doesn't work the same (just something to note).

    But now I realize I kinda need to combine the tutorial result, and what I just did.

    I want the player to turn whenever I press a corresponding key. But if I'm walking diagonally and then let go of a key, I want the player to face the remaining key's direction (like it does in the tutorial). Currently tried all sorts of combinations using two conditions per transition, but can't get it working so far.