![]() |
Attention | Topic was automatically imported from the old Question2Answer platform. |
![]() |
Asked By | darkdestiny1010 |
Currently, I’m trying to use a piece of code to decrease the player’s y position to get them to jump, but I am running into a bit of an issue. With my current code (which is currently set up so that in the future I can add more mechanics) I’m using a state for a function, which doesn’t seem to work. However, I tested it with _physics_process(delta) and it worked then. I would like to keep the state machine, so I wanted to check here if there was a solution. Here’s the code:
extends KinematicBody2D
const ACCELERATION = 500
const MAX_SPEED = 80
const FRICTION = 500
const GRAVITY = 20
const FALL_SPEED = 200
const JUMP_FORCE = 1000
enum {
MOVE,
BLINK,
SHOOT
}
var state = MOVE
var velocity = Vector2.ZERO
func _physics_process(delta):
match state:
MOVE:
move_state(delta)
BLINK:
pass
SHOOT:
pass
func move_state(delta):
var input_vector = Vector2.ZERO
input_vector.x = Input.get_action_strength("ui_right") - Input.get_action_strength("ui_left")
input_vector = input_vector.normalized()
if input_vector != Vector2.ZERO:
velocity = velocity.move_toward(input_vector * MAX_SPEED, ACCELERATION * delta)
else:
velocity = velocity.move_toward(Vector2.ZERO, FRICTION * delta)
velocity.y += GRAVITY
if velocity.y > FALL_SPEED:
velocity.y = FALL_SPEED
if is_on_floor():
if Input.is_action_just_pressed("jump"):
velocity.y = -JUMP_FORCE
velocity = move_and_slide(velocity)
Any assistance with this will be appreciated. Thank you.