Move & Jump Functionality in 2D Platformer

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By Souvlaki42

I’m trying to make my 2d character move and jump when a or d pressed the same time as space pressed. I think I have tried anything but I can’t find the solution. What I did wrong?

EDIT:


extends KinematicBody2D

onready var sprite = $Sprite
onready var collision = $Collision
onready var mapManager = get_parent().get_node("RoomManager")

var acceleration
var friction
var baseSpeed
var speed
var jumpPower
var gravity
var state
var level
var room
var moveVec
var velocity
var climbing
var health

var rng = RandomNumberGenerator.new()
var gameSeed = 0
var lastStep = 0

export (bool) var canFall = true
export (bool) var canCollide = true
var onFloor = true

enum {IDLE, WALK, RUN, JUMP, FALL, CROUCH, LADDER_CLIMB, SLIDE_START, SLIDE_LOOP, SLIDE_END,
WALL_SLIDE, ATTACK, COMBO, CRITICAL, SPELL, SHIELD, DAMAGE, DEATH, WALK_JUMP_LEFT, WALK_JUMP_RIGHT}

func _ready():
	start()

func start():
	if canCollide:
		collision.disabled = false
	else:
		collision.disabled = true
	moveVec = Vector2(1,0)
	velocity = Vector2.ZERO
	climbing = false
	health = 100
	state = IDLE
	level = 1
	room = 1
	if gameSeed == 0:
		rng.randomize()
	else:
		rng.set_seed(gameSeed)
	acceleration = 500
	friction = 700
	baseSpeed = 100
	speed = baseSpeed
	jumpPower = 600
	gravity = 50
	
func _physics_process(delta):
	stateMachine(delta)
	
func stateMachine(delta):
	particles()
	inputs()
	position.x = clamp(position.x , -1, 337)
	match state:
		IDLE:
			idle_state(delta)
		WALK:
			walk_state(delta)
		RUN:
			run_state(delta)
		JUMP:
			jump_state()
		FALL:
			fall_state()
		CROUCH:
			crouch_state()
		LADDER_CLIMB:
			ladder_climb_state()
		SLIDE_START:
			slide_start_state()
		SLIDE_LOOP:
			slide_loop_state()
		SLIDE_END:
			slide_end_state()
		WALL_SLIDE:
			wall_slide_state()
		ATTACK:
			attack_state()
		COMBO:
			combo_state()
		CRITICAL:
			critical_state()
		SPELL:
			spell_state()
		SHIELD:
			shield_state()
		DAMAGE:
			damage_state()
		DEATH:
			death_state()
	velocity = move_and_slide(velocity, Vector2.UP)
	if is_on_floor():
		onFloor = true
	else:
		onFloor = false
	if not is_on_floor() and canFall:
		velocity.y += gravity
	
func particles():
	if velocity.x == 0:
		lastStep = -1
	if sprite.animation == "Run":
		if sprite.frame == 0 or sprite.frame == 3:
			if lastStep != sprite.frame:
				lastStep = sprite.frame
				var footstep = Preloads.dustParticle.instance()
				footstep.emitting = true
				footstep.global_position = Vector2(global_position.x, global_position.y)
				get_parent().add_child(footstep)
	
func inputs():
	if Input.is_action_pressed("left") and not Input.is_action_pressed("run"):
		state = WALK
		sprite.flip_h = true
		moveVec.x = -1
	elif Input.is_action_pressed("right") and not Input.is_action_pressed("run"):
		state = WALK
		sprite.flip_h = false
		moveVec.x = 1
	elif Input.is_action_pressed("left") and Input.is_action_pressed("run"):
		state = RUN
		sprite.flip_h = true
		moveVec.x = -1
	elif Input.is_action_pressed("right") and Input.is_action_pressed("run"):
		state = RUN
		sprite.flip_h = false
		moveVec.x = 1
	elif Input.is_action_pressed("crouch") and Input.is_action_pressed("rightclick"):
		state = SHIELD
	elif Input.is_action_pressed("crouch"):
		state = CROUCH
	elif Input.is_action_pressed("slide"):
		state = SLIDE_START
	elif Input.is_action_pressed("up") and climbing:
		state = LADDER_CLIMB
	else:
		state = IDLE
		
	if Input.is_action_just_pressed("jump"):
		state = JUMP

func idle_state(delta):
	sprite.play("Idle")
	velocity = velocity.move_toward(Vector2.ZERO, friction * delta)
	
func walk_state(delta):
	sprite.play("Walk")
	speed = baseSpeed
	velocity = velocity.move_toward(moveVec * speed, acceleration * delta)
	
func run_state(delta):
	sprite.play("Run")
	speed = baseSpeed * 2
	velocity = velocity.move_toward(moveVec * speed, acceleration * delta)
	
func jump_state():
	if onFloor:
		sprite.play("BeforeJump")
		sprite.play("Fly")
		velocity.y -= jumpPower
		sprite.play("ReloadJump")
		state = FALL

func fall_state():
	sprite.play("Fall")
	velocity.y += gravity
	if is_on_floor():
		sprite.play("Land")
		
func crouch_state():
	sprite.play("Crouch")
	velocity = Vector2.ZERO
	
func ladder_climb_state():
	pass
	
func slide_start_state():
	pass
	
func slide_loop_state():
	pass
	
func slide_end_state():
	pass
	
func wall_slide_state():
	pass
	
func attack_state():
	pass
	
func combo_state():
	pass
	
func critical_state():
	pass
	
func spell_state():
	pass
	
func shield_state():
	sprite.play("Shield")
	
func damage_state():
	pass
	
func death_state():
	pass
:bust_in_silhouette: Reply From: aXu_AP

Your if branches are exclusive. Ie. code is checking if you are pressing right, walk right. If not, check if jump is pressed. This is easily fixed by changing elif to if:

if Input.isactionjustpressed("jump"):
    state = JUMP

You might want to do something similiar with crouching etc… Think what cases should be possible together, which not. Or move higher priority ones higher in the if cases (check jump first, then walk).

it didn’t worked

Souvlaki42 | 2022-03-10 23:13