I Need Help with diagonal idle face direction

I have been struggling on trying to get a similar idle moment like this.

7965d39907a7c76d29ffc0e6e1496ff2-ezgif.com-video-to-gif-converter

So if you’re moving in the direction (1,1) I want it to play the idle animation for facing right. I understand the problem which is one of the keys is being let go like a .1 second too early resulting in only getting one direction like (0,1) or (1,0). I’m struggling to find a solution, I attempted adding a delay to when input should be stored so you have to hold the keys for a bit before storing the last vector, but that also didn’t work completely, b/c if you tap to move in a diagonal direction it looks odd. Realistically, I know someone wouldn’t play that way, but I still want it to look right and feel right.

Any tutorial I find on diagonal moment completely ignore this problem and it’s hard to find a solution. I would really appreciate it if someone could give me an idea of how to tackle this problem. Thank you!

You might solve it the way you already tried, but:

  • when the count of keys held down are increased, then do the direction change instantly
  • when key count goes down as keys are released, delay the direction change slightly, which would allow the fingers to release one first, then the other, while maintaining the diagonal motion.

There still will be a “lip” though, but you can address that by experimenting with the time interval.

Getting movement working and getting movement GOOD are two different things.

Tutorials for “how to make character controllers” are a little bit like tutorials for “How to make your very own four-wheeled motorized vehicle.” Yeah, you’ll have a motorized vehicle, but it won’t be going on the motorway anytime soon.

1 Like

I think I understand what you mean. So I’m understanding correctly you’re saying I should have two separate floats that will count up for however long the key is pressed, then when it’s released count down and wait a bit before changing directions?

So like if I’m going (1,1) then if the D key is released a bit before W, wait a bit so that (0,1) doesn’t get stored into my lastDir variable?

And that’s fair about the tutorial thing, I understand they’re there to push you in the right direction so I shouldn’t expect every answer to be given to me. It is a learning process after all.

So I managed to do what I wanted to do.
I’m sure it’s not the best way to do it, but with what I know this is how I managed to do it. Every time I test it I’m getting the right direction even for the diagonal directions.

public class pm : MonoBehaviour
{
    private float dirX;
    private float dirY;
    public Vector2 dir;
    public Vector2 lastDir;
    public Vector2 finalDir;
    public Vector2 moveDir;
    
    public float topRightTimePressed;
    public float topLeftTimePressed;
    public float botRightTimePressed;
    public float botLeftTimePressed;

    public bool topRightRecent = false;
    public bool topLeftRecent = false;
    public bool botRightRecent = false;
    public bool botLeftRecent = false;

    public float resetTime;
 

    private void Update()
    {
        dirX = Input.GetAxisRaw("Horizontal");
        dirY = Input.GetAxisRaw("Vertical");

        if ((dirX == 0 && dirY == 0) && moveDir.x != 0 || moveDir.y != 0)
        {
            lastDir = moveDir;
        }
        else if ((lastDir == Vector2.up || lastDir == Vector2.right) && (topRightTimePressed > 0)) // Top Right
        {
            lastDir = Vector2.one;
        }
        else if ((lastDir == Vector2.up || lastDir == Vector2.left) && (topLeftTimePressed > 0)) // Top Left
        {
            lastDir = new Vector2 (-1, 1);
        }
        else if ((lastDir == Vector2.down || lastDir == Vector2.right) && (botRightTimePressed > 0)) // Bottom Right
        {
            lastDir = new Vector2(1, -1);
        }
        else if ((lastDir == Vector2.down || lastDir == Vector2.left) && (botLeftTimePressed > 0)) // Bottom Left
        {
            lastDir = new Vector2(-1, -1);
        }

        moveDir = new Vector2(dirX, dirY);

        if (moveDir == Vector2.one) // Top Right Section
        {
            // Reset all the other timers but for this direction here
            topLeftTimePressed = 0;
            botRightTimePressed = 0;
            botLeftTimePressed = 0;
            topRightRecent = true;
            
        }
        if (topRightRecent)
        {
            topRightTimePressed += Time.deltaTime;
        }
        if (topRightTimePressed >= resetTime)
        {
            topRightRecent = false;
            topRightTimePressed = 0;
        }

        /////

        if (moveDir == new Vector2(-1, 1)) // Top Left Section
        {
            // Reset all the other timers but for this direction here
            topRightTimePressed = 0;
            botRightTimePressed = 0;
            botLeftTimePressed = 0;
            topLeftRecent = true;

        }
        if (topLeftRecent)
        {
            topLeftTimePressed += Time.deltaTime;
        }
        if (topLeftTimePressed >= resetTime)
        {
            topLeftRecent = false;
            topLeftTimePressed = 0;
        }

        /////

        if (moveDir == new Vector2(1, -1)) // Bottom Right Section
        {
            // Reset all the other timers but for this direction here
            topRightTimePressed = 0;
            topLeftTimePressed = 0;
            botLeftTimePressed = 0;
            botRightRecent = true;

        }
        if (botRightRecent)
        {
            botRightTimePressed += Time.deltaTime;
        }
        if (botRightTimePressed >= resetTime)
        {
            botRightRecent = false;
            botRightTimePressed = 0;
        }

        /////

        if (moveDir == new Vector2(-1, -1)) // Bottom Left Section
        {
            // Reset all the other timers but for this direction here
            topRightTimePressed = 0;
            topLeftTimePressed = 0;
            botRightTimePressed = 0;
            botLeftRecent = true;

        }
        if (botLeftRecent)
        {
            botLeftTimePressed += Time.deltaTime;
        }
        if (botLeftTimePressed >= resetTime)
        {
            botLeftRecent = false;
            botLeftTimePressed = 0;
        }



    }
}