Help, how to fix this? Player walking issue!

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

If you want the player to shoot while running, you need to blend the animations. If you want the player to stop moving when they shoot, you need to stop them from moving.

So what do you want to happen?

I just want to stop the sliding of the player that’s all ,but i don’t know how to do it exactly

If i press shoot then hold left or right key ,the player keep sliding without walk animation playing

You code is only checking if an action was just pressed or released, you will need more complex logic for mixed states such as walking and shooting.

For now you could add another if statement to determine if they are walking

if Input.is_action_just_released("shoot"):
	if velocity.is_zero_approx():
		sprite_2d.animation = "idle"
	else:
		sprite_2d.animation = "right" if velocity > 0 else "left"
	is_shooting = false
2 Likes