Attention | Topic was automatically imported from the old Question2Answer platform. | |
Asked By | Ace The Dolphin Guy |
Hello
I am trying to making a simple 2D platformer with a run mechanic where if your press up the speed increases. When I jump while running to the right it works fine. But if I’m moving right while ruining and try to jump, the character won’t jump and just fall back down. I’m relatively new to coding and I’m not sure whats cause this behavior. Any help with explaining why this happens and what do do differently would be great!
Here is the script that does all the moment
extends KinematicBody2D
const UP = Vector2(0, -1)
export var Walk_Speed = 350
export var Run_Speed = 670
export var Gravity = 20
export var Jump_Force = 600
var location = Vector2()
var run = false
func _physics_process(delta):
#Run
if Input.is_action_pressed("ui_up"):
run = true
else:
run = false
#Jump
if is_on_floor():
if Input.is_action_pressed("ui_accept"):
location.y = -Jump_Force
#Both Input
if Input.is_action_pressed("ui_left") and Input.is_action_pressed("ui_right"):
location.x = 0
#Right Input
elif Input.is_action_pressed("ui_right"):
if run:
location.x = Run_Speed
else:
location.x = Walk_Speed
#Left Input
elif Input.is_action_pressed("ui_left"):
if run:
location.x = -Run_Speed
else:
location.x = -Walk_Speed
#No Input
else:
location.x = 0
#Gravity
location.y += Gravity
location = move_and_slide(location, UP)
Thanks, Ace : )