Godot Version
Godot 4.7.1
Question
Hello, I’m starting programming with Godot and I’m very new in this so I’m sorry if I ask for clarification on certain things, well the problem is that i made a shoot system for my character, it worked fine until I wanted to add up and down shoots, I want to shoot when the player press the E button and shoot down or up when the player press the E button AND the S or W button, when I tried to do it, the character began to shoot the Horizontal Shoot and the Down Shoot, I would appreciate some help thanks.
hard to say what the problem is, can you share some relevant code?
You would need to check if two keys are pressed at the same time. E.g.
func _process(delta):
if Input.is_key_pressed(KEY_E) and Input.is_key_pressed(KEY_W):
print("E and W keys are pressed concurrently.")
if Input.is_key_pressed(KEY_E) and Input.is_key_pressed(KEY_S):
print("E and S keys are pressed concurrently.")
Orsomething like this.
func _process(delta):
var a_held = Input.is_action_pressed("action_a")
var b_held = Input.is_action_pressed("action_b")
var a_just = Input.is_action_just_pressed("action_a")
var b_just = Input.is_action_just_pressed("action_b")
# Triggers if A is held and B was just clicked, or vice-versa
if (a_just and b_held) or (b_just and a_held):
trigger_combo_action()
Disable horizontal shooting if up or down are also pressed.
Sure, This is all the code for the character including the shoot system and its movement system (thanks btw)
extends CharacterBody2D
@onready var Ray = $AnimatedSprite2D
@onready var Laser = preload("res://Laser.tscn")
@onready var Laser_y = preload("res://Laser_Y.tscn")
var SPEED = 300.0
var JUMP_VELOCITY = -300.0
var shoot_x : bool
func _physics_process(delta: float) -> void:
# Add the gravity.
if velocity.x > 1 or velocity.x < -1:
Ray.play("Right_Walk")
#if velocity.x < -1:
#Ray.play("Left_Walk")
if velocity.x == 0:
Ray.play("Right_Idle")
if not is_on_floor():
Ray.play("Jump")
velocity += get_gravity() * delta
# Handle jump.
if Input.is_action_pressed("Jump") and is_on_floor():
velocity.y = JUMP_VELOCITY
# Get the input direction and handle the movement/deceleration.
# As good practice, you should replace UI actions with custom gameplay actions.
var direccion := Input.get_axis("Left_Walk", "Right_Walk")
if direccion:
velocity.x = direccion * SPEED
else:
velocity.x = move_toward(velocity.x, 0, SPEED)
move_and_slide()
if direccion == 1.0:
Ray.flip_h = false
elif direccion == -1.0:
Ray.flip_h = true
if Input.is_action_just_pressed("Shoot"):
var Laser_temp = Laser.instantiate()
if Ray.flip_h == false:
Laser_temp.direction = 1
if Ray.flip_h == true:
Laser_temp.direction = -1
add_child(Laser_temp)
if Input.is_action_pressed("Aim_Down") and Input.is_action_just_pressed("Shoot_y"):
var Laser_temp_y = Laser_y.instantiate()
Laser_temp_y.direction = 1
add_child(Laser_temp_y)
Is your problem that it shoots twice? It looks like these if statements are not exclusive so there is potential to shoot in both directions, especially so if “Shoot_y” is mapped to the same input as “Shoot”
yeah thats the problem its shoots in both directions when its supposed to only shoot Down
Then you need to order and chain your if statements so it matches what you want to happen. Consecutive if/elif/else branches will only process one at a time
if Input.is_action_just_pressed("Shoot")
if Input.is_action_pressed("Aim_Down"):
# shoot down
elif Input.is_action_pressed("Aim_Up"):
# shoot up
else:
# shoot normal
Thank you, this has been very helpful 