I am reworking my charater movement and decided to create a state machine. I got the following states:
- PlayerIdle
- PlayerWalk
- PlayerAttack
The states do basically what they are saying, the PlayerIdle is the init state and also when nothing is done or pressed, the PlayerWalk handles the move animation and the velocity and PlayerAttack makes the player perform a swing attack. In the PlayerIdle and PlayerWalk state, the button for attacking is checked so when you either stand still or walk around and press the Attack Button, the statemachine jumps to the Attack state and the player performs an Attack.
But, the wierd thing I came across which I cant figure out it the following:
When standing still and looking at any direction (So in the Idle State) the attack will be performed when pressing the attack button
When walking either left, right, up or down, same thing, the attack will be performed.
When walking diagonal only running up and left will make the Input.is_action_just_pressed("Attack") function return true (so it attacks). Going the other three diagonal direction the function returns false and the player will not attack.
My question: How does the function only returns true if the player walks up and left and not the other diagonal ways?
Code in the PlayerWalk State looks as follows:
var input_vector: Vector2 = Input.get_vector("ui_left", "ui_right", "ui_up", "ui_down")
if Input.is_action_just_pressed("attack"):
if !player.isAttacking:
player.FSM.change_state(self, "PlayerAttack")
return
if input_vector == Vector2.ZERO:
player.FSM.change_state(self, "PlayerIdle")
else:
updateWalkingAnimation(input_vector)