How Do I go about interrupting an action?

How Do I go about interrupting an action, when i press another button going a different direction, not so that the player goes in that direction, just so it will stop the action…

Ill give you 2 bits of code because im feeling really dumb right now. and you can see what im working with…

    // Update is called once per frame
    void Update()
    {
        switch (state)
        {
            case State.Normal:
                Move();
                Sprinting();
                DodgeRolling();
                break;
            case State.Sprinting:
                HandleSprinting();
                break;
            case State.Rolling:
                HandleDodgeRoll();
                break;
        }

this here^^ determains my state so i need this to change as well…

    private void Inputs()
    {
        input = Vector3.zero;
        input.x = Input.GetAxisRaw("Horizontal");
        input.y = Input.GetAxisRaw("Vertical");

        if (input.x != 0) input.y = 0;
    }

    public void Move()
    {
        Inputs();

        if (input != Vector3.zero)
        {
            myRigidbody.MovePosition(transform.position + input * walk * Time.deltaTime);
        }
    }

^^This is for my movement which doesnt really affect my rolling but I use this to determain the direction and i want this to also interrupt the rolling

public void HandleDodgeRoll()
    {
        myRigidbody.MovePosition(transform.position += input * rollSpeed * Time.deltaTime);

        isDodging = true;
        rollSpeed -= rollSpeed * 8 * Time.deltaTime;

        if (rollSpeed < 8)
        {
            state = State.Normal;
            rollSpeed = roll;
            isDodging = false;
        }
    }

I guess while I’m at it, how do I change states while in one state i.e. sprinting to roll and vice versa while in one of the 2 states?

@iR3dyPlayZ

It looks like you are potentially creating a blocking state with your state machine, which would prevent interruption of other states. I would move Inputs() outside of the state machine and probably implement a StateHandler() method prior to the switch statement.

Assuming the variables being referenced in HandleDodogeRoll() are class variables, StateHandler() could set the state and variables as needed per state.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

    [RequireComponent(typeof(Rigidbody2D))]
    [RequireComponent(typeof(BoxCollider2D))]
    public class PlayerController : MonoBehaviour
    {
        private Rigidbody2D playerRigidbody;
        private Vector3 input;
    
        public float walkSpeed = 4;
        float walk;
    
        public float sprintSpeed = 7;
        float sprint;
        public bool isSprinting;
    
        public float rollSpeed = 24;
        float roll;
        public bool isDodging;
    
        private State state;
    
        // Start is called before the first frame update
        void Start()
        {
            playerRigidbody = GetComponent<Rigidbody2D>();
    
            walk = walkSpeed;
            roll = rollSpeed;
            sprint = sprintSpeed;
    
        }
    
        private enum State
        {
            Normal, Rolling, Sprinting,
        }
    
        private void Awake()
        {
            state = State.Normal;
        }
    
        // Update is called once per frame
        void Update()
        {
            switch (state)
            {
                case State.Normal:
                    Move();
                    Sprinting();
                    DodgeRolling();
                    break;
                case State.Sprinting:
                    HandleSprinting();
                    break;
                case State.Rolling:
                    HandleDodgeRoll();
                    break;
            }
        }
    
        private void Inputs()
        {
            input = Vector3.zero;
    
            //Horizontal Movement
            if (Input.GetKey(KeyCode.A))
            {
                input.x = -1;
            }
            if (Input.GetKey(KeyCode.D))
            {
                input.x = 1;
            }
    
            //Vertical Movement
            if (Input.GetKey(KeyCode.S))
            {
                input.y = -1;
            }
            if (Input.GetKey(KeyCode.W))
            {
                input.y = 1;
            }
    
            if (input.x != 0) input.y = 0;
        }
    
        public void Move()
        {
            Inputs();
    
            if (input != Vector3.zero)
            {
                playerRigidbody.MovePosition(transform.position + input * walk * Time.deltaTime);
            }
        }
    
        public void Sprinting()
        {
    
            if(Input.GetKeyDown(KeyCode.LeftShift) && input.x == 1 | input.y == 1 | input.x == -1 | input.y == -1)
            {
                state = State.Sprinting;
            }
        }
    
        public void HandleSprinting()
        {
            Inputs();
    
            if (input != Vector3.zero)
            {
                playerRigidbody.MovePosition(transform.position + input * sprint * Time.deltaTime);
            }
    
            isSprinting = true;
    
            if (Input.GetKeyUp(KeyCode.LeftShift))
            {
                state = State.Normal;
                isSprinting = false;
            }
        }
    
        public void DodgeRolling()
        {
            if (Input.GetKeyDown(KeyCode.RightControl) && input.x == 1 | input.y == 1 | input.x == -1 | input.y == -1)
            {
                state = State.Rolling;
            }
        }
    
        public void HandleDodgeRoll()
        {
            playerRigidbody.MovePosition(transform.position += input * rollSpeed * Time.deltaTime);
    
            isDodging = true;
            rollSpeed -= rollSpeed * 8 * Time.deltaTime;
    
            if (rollSpeed < 8)
            {
                state = State.Normal;
                rollSpeed = roll;
                isDodging = false;
            }
        }
    }