Godot Version
4.6.1
Question
How do I make it so if the player is on a specific part of a tile, it plays the lake sound.
extends CharacterBody2D
const SPEED = 450.0
const JUMP_VELOCITY = -1000.0
@onready var swing: AudioStreamPlayer2D = $swing
var is_jumping = false
var is_attacking = false
@onready var animated_sprite_2d: AnimatedSprite2D = $AnimatedSprite2D
@onready var jumpsound: AudioStreamPlayer2D = $jumpsound
@onready var step: AudioStreamPlayer2D = $step
var step_timer := 0.0
var step_interval := 0.35
func _physics_process(delta: float) -> void:
if not is_on_floor():
velocity += get_gravity() * delta
if Input.is_action_just_pressed("jump") and is_on_floor() and not is_attacking:
velocity.y = JUMP_VELOCITY
jumpsound.pitch_scale = randf_range(0.9, 1.1)
jumpsound.play()
is_jumping = true
if Input.is_action_just_released("jump") and velocity.y < 0:
velocity.y *= 0.4
if Input.is_action_just_pressed("attack") and not is_attacking:
is_attacking = true
swing.pitch_scale = randf_range(0.8, 1.2)
swing.play()
animated_sprite_2d.play("attack")
animated_sprite_2d.animation_finished.connect(_on_attack_finished)
var direction := Input.get_axis("left", "right")
if direction:
velocity.x = direction * SPEED
if is_on_wall():
step_timer = 0.0
else:
if is_on_floor():
step_timer -= delta
if step_timer <= 0:
step.play()
step_timer = step_interval
else:
velocity.x = move_toward(velocity.x, 0, SPEED)
step_timer = 0.0
move_and_slide()
if direction == 1:
animated_sprite_2d.flip_h = false
if direction == -1:
animated_sprite_2d.flip_h = true
if is_attacking:
return
if not is_on_floor():
animated_sprite_2d.play("jump")
is_jumping = true
else:
is_jumping = false
if abs(velocity.x) > 1:
animated_sprite_2d.play("run")
else:
animated_sprite_2d.play("idle")
func _on_attack_finished() -> void:
animated_sprite_2d.animation_finished.disconnect(_on_attack_finished)
is_attacking = false
