8 direction roll not working in some directions

Hi all,

I’m trying to make a basic top down 8 direction player controller.
It’s modified from a tutorial I found a while ago to include a roll function. The roll works in the cardinal directions and diagonally up and right or down and left. For some reason however, the roll doesn’t do anything when moving down and right or up and left. Initially I thought that might have something to do with the positive and negative x and y cancelling each other out somehow but can’t figure out why that would occur. Also the debug (which I’ve left in below) says that the problem relates to recognising the input of jump being pressed (step 2 of the debug isn’t firing). So now I have no idea what to do. Tried building a dummy version to see if it was a weird issue in the editor.

Anyone have any suggestions?

public class PlayerController : MonoBehaviour
{
    public float moveSpeed = 60f;
    public float dodgeSpeed = 250f;
    public Vector3 attackDir = new Vector3(1, 0, 0);
    private enum State
    {
        Normal,
        Rolling,
        Paused,
        Attacking,
    }
    private Rigidbody2D rb;
    private Vector3 moveDir;
    private Vector3 rollDir;
    private Vector3 lastMoveDir;
    private float rollSpeed;
    private State state;
    private void Awake()
    {
        rb = GetComponent<Rigidbody2D>();
        state = State.Normal;
    }
    private void Update()
    {
        switch (state)
        {
            case State.Normal:
                float moveX = 0f;
                float moveY = 0f;
                moveX = Input.GetAxisRaw("Horizontal");
                moveY = Input.GetAxisRaw("Vertical");
                moveDir = new Vector3(moveX, moveY).normalized; 
                lastMoveDir = moveDir;  //remember what direction you're facing
                Debug.Log("Step 1: lastmovedir = : " + lastMoveDir);
                if (moveDir != Vector3.zero)
                    attackDir = moveDir; //look in the direction of the last input
                if (Input.GetButtonDown("Jump")) //if roll is pressed
                {
                    Debug.Log("Step 2: jump pressed");
                    rollDir = lastMoveDir;      //roll in direction of movement
                    Debug.Log("Step 3: rolldir = " + rollDir);
                    rollSpeed = dodgeSpeed;
                    state = State.Rolling; 
                }
                break;
            case State.Rolling:             
                float rollSpeedDropMultiplier = 5f; //amount to reduce roll by each second
                rollSpeed -= rollSpeed * rollSpeedDropMultiplier * Time.deltaTime;
                float rollSpeedMinimum = (dodgeSpeed/5);    //the point at which you stop rolling. Playing around, /5 seems to be smoothest
                if (rollSpeed < rollSpeedMinimum)
                {
                    state = State.Normal;
                }
                break;
        }
    }
    private void FixedUpdate()
    {
        switch (state)
        {
            case State.Normal:
                rb.velocity = moveDir * moveSpeed;
                break;
            case State.Rolling:
                rb.velocity = rollDir * rollSpeed;
                break;
        }
    }
}

Your code appears reasonable. It could just be a limitation of your keyboard, also known as key rollover.

Try mapping the Jump button to something else temporarily, or cloning that InputManager entry so you have two Jump buttons, and see if the rolling diagonal can perhaps function with another key.

Another way to isolate this problem is to hard-code the motion diagonal you can’t get, making your player always move this way, then see if Jump/Roll works. If so, that likely points to rollover being the culprit.

Hmm, yeah turns out you’re right. Thanks a lot!
Hadn’t come across that term before. Haha, wasted more time than I’m ready to admit trying to solve a problem that didn’t exist…

Well, don’t be too rough on yourself… the problem EXISTS, it’s just in another castle, the hardware castle.