+1 vote

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 = getparent().getnode("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, LADDERCLIMB, SLIDESTART, SLIDELOOP, SLIDEEND,
WALLSLIDE, ATTACK, COMBO, CRITICAL, SPELL, SHIELD, DAMAGE, DEATH, WALKJUMPLEFT, WALKJUMP_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 physicsprocess(delta):
stateMachine(delta) func stateMachine(delta):
particles()
inputs()
position.x = clamp(position.x , -1, 337)
match state:
IDLE:
idlestate(delta)
WALK:
walk
state(delta)
RUN:
runstate(delta)
JUMP:
jump
state()
FALL:
fallstate()
CROUCH:
crouch
state()
LADDERCLIMB:
ladder
climbstate()
SLIDE
START:
slidestartstate()
SLIDELOOP:
slide
loopstate()
SLIDE
END:
slideendstate()
WALLSLIDE:
wall
slidestate()
ATTACK:
attack
state()
COMBO:
combostate()
CRITICAL:
critical
state()
SPELL:
spellstate()
SHIELD:
shield
state()
DAMAGE:
damagestate()
DEATH:
death
state()
velocity = moveandslide(velocity, Vector2.UP)
if isonfloor():
onFloor = true
else:
onFloor = false
if not isonfloor() 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.globalposition = Vector2(globalposition.x, globalposition.y)
get
parent().add_child(footstep) func inputs():
if Input.isactionpressed("left") and not Input.isactionpressed("run"):
state = WALK
sprite.fliph = true
moveVec.x = -1
elif Input.is
actionpressed("right") and not Input.isactionpressed("run"):
state = WALK
sprite.flip
h = false
moveVec.x = 1
elif Input.isactionpressed("left") and Input.isactionpressed("run"):
state = RUN
sprite.fliph = true
moveVec.x = -1
elif Input.is
actionpressed("right") and Input.isactionpressed("run"):
state = RUN
sprite.flip
h = false
moveVec.x = 1
elif Input.isactionpressed("crouch") and Input.isactionpressed("rightclick"):
state = SHIELD
elif Input.isactionpressed("crouch"):
state = CROUCH
elif Input.isactionpressed("slide"):
state = SLIDESTART
elif Input.is
actionpressed("up") and climbing:
state = LADDER
CLIMB
else:
state = IDLE if Input.is_action_just_pressed("jump"): state = JUMP

func idlestate(delta):
sprite.play("Idle")
velocity = velocity.move
toward(Vector2.ZERO, friction * delta)

func walkstate(delta):
sprite.play("Walk")
speed = baseSpeed
velocity = velocity.move
toward(moveVec * speed, acceleration * delta)

func runstate(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 fallstate():
sprite.play("Fall")
velocity.y += gravity
if is
on_floor():
sprite.play("Land")

func crouch_state():
sprite.play("Crouch")
velocity = Vector2.ZERO

func ladderclimbstate():
pass

func slidestartstate():
pass

func slideloopstate():
pass

func slideendstate():
pass

func wallslidestate():
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

Godot version v3.4.3.stable.mono.official
in Engine by (17 points)
edited by

1 Answer

0 votes

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).

by (1,100 points)

it didn't worked

Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read Frequently asked questions and How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to [email protected]gine.org with your username.