- Home /
 
The question is answered, right answer was accepted
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?
Answer by djenningsais · Mar 02, 2021 at 03:08 AM
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.
@djenningsais I'm a little lost to tell you the truth i pulled this out of my a**, from watching tutorials and trying to implement things in my own way... and I think im doing alright so far.
here's the whole script, can you show me what you mean and explain you changes if its not a bother.
I posted the script as an answer
Also How do i make my character face the direction it's moving??
Answer by iR3dyPlayZ · Mar 02, 2021 at 03:27 AM
 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;
             }
         }
     }
     
 
 
              I had to look up RequireComponent, that is pretty cool.
Please see my recommended changes below:
 void Update()
     {
         //hiding Inputs() under a sub method will block further input
         Inputs();
 
         switch (state)
         {
             case State.Normal:
                 Move();
                 //Sprinting();  - move this functionality to Inputs()
                 //DodgeRolling();  - move this functionality to Inputs()
                 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;
 
         //set Sprinting
         if (Input.GetKeyDown(KeyCode.LeftShift) && input.x == 1 | input.y == 1 | input.x == -1 | input.y == -1)
         {
             state = State.Sprinting;
         }
 
         //set Rolling
         if (Input.GetKeyDown(KeyCode.RightControl) && input.x == 1 | input.y == 1 | input.x == -1 | input.y == -1)
         {
             state = State.Rolling;
         }
 
     }
 
                 For rotation I would look into Rigidbody2D.transform.rotation.
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
Initialising List array for use in a custom Editor 1 Answer
Health script, save health in between scenes. PlayerPrefs. 2 Answers