Godot 4,3 `
Question
Hi!
`when i press right or left key the player walk fine with the animation playing, but when i press shoot then hold walk button this what happen the player keep sliding no walk animation is playing until i release the walk button and press again then the animation plays
Player script
var is_shooting := false
const SPEED = 500
const JUMP_VELOCITY = -750.0
func _physics_process(delta: float) -> void:
HP()
handle_hp_regen(delta)
# Add the gravity.
if not is_on_floor():
velocity += get_gravity() * delta
# Handle jump.
if Input.is_action_just_pressed("jump") and is_on_floor():
velocity.y = JUMP_VELOCITY
sprite_2d.animation = "jump"
hero_jump.play()
player_jump.play()
if Input.is_action_just_released("jump"):
sprite_2d.animation = "idle"
# Get the input direction and handle the movement/deceleration.
if not is_shooting:
var direction := Input.get_axis("left", "right")
if direction != 0:
# Flip the sprite and adjust muzzle position
$Sprite2d.flip_h = direction < 0
$Muzzle/FireFlash.flip_h = direction < 0
$gun_smoke.flip_h = direction < 0
# Adjust muzzle position based on facing direction
if direction < 0: # Facing left
muzzle.position.x = -100 # Your specified offset
else: # Facing right
muzzle.position.x = -24 # Reset to positive value (original position)
velocity.x = direction * SPEED
else:
velocity.x = move_toward(velocity.x, 0, SPEED)
move_and_slide()
if Input.is_action_just_pressed("shoot"):
shoot()
bowShoot.play()
sprite_2d.animation = "shoot"
is_shooting = true
if Input.is_action_just_released("shoot"):
sprite_2d.animation = "idle"
is_shooting = false
if Input.is_action_just_pressed("shoot2"):
shoot2()
typhon.play()
gun_smoke.play()
sprite_2d.animation = "shoot2"
is_shooting = true
fire_flash.play()
if Input.is_action_just_released("shoot2"):
sprite_2d.animation = "idle_2"
is_shooting = false
if Input.is_action_just_pressed("shoot3"):
shoot3()
sprite_2d.animation = "shoot3"
is_shooting = true
gun_flash.play()
missile_sound.play()
if Input.is_action_just_released("shoot3"):
sprite_2d.animation = "idle_3"
is_shooting = false
# Animations
if Input.is_action_just_pressed("left"):
sprite_2d.animation = "left"
player_walk.play()
if Input.is_action_just_released("left"):
sprite_2d.animation = "idle"
if Input.is_action_just_pressed("right"):
sprite_2d.animation = "right"
player_walk.play()
if Input.is_action_just_released("right"):
sprite_2d.animation = "idle"
func shoot2():
var c = preload ("res://bullet2.tscn").instantiate()
c.direction = -1 if sprite_2d.flip_h else 1
get_tree().root.add_child(c)
c.transform = $Muzzle.global_transform
vibrate(VibrationType.MEDIUM) # Medium vibration for this weapon
typhon.play()
gun_smoke.play()
sprite_2d.animation = "shoot2"
is_shooting = true
fire_flash.play()