Godot Version
v4.3.stable.official [77dcf97d8]
Question
I am currently working through a tutorial to learn Godot 4, so please be patient with me, I don’t know what I’m doing yet!
So far, I am trying to make my 2D player character play sound effects when it does certain actions. Following the tutorial, I got run and landing animations set up with sound effects working as they should. Using the same steps, I tried getting the jump set up. It works in the preview, but not in the main scene. I’ve been testing out different settings to see what things do, and I switched it from “blend” to “don’t use blend” and noticed it played the jump sound at the very beginning of playing the main scene and not when I jump. (Not sure if that’s relevant at all, but I figured I’d mention it.) I’ve tried different sound files as well as deleting the key frame and adding a new one.
Does anyone have any idea where things might be going wrong? I don’t think it’s something in my scripts, but I’ll attach what I have here just in case:
extends CharacterBody2D
@export_category("Locomotion")
@export var _speed : float = 8
@export var _acceleration : float = 16
@export var _deceleration : float = 32
@export_category("Jump")
@export var _air_control : float = 0.75
@export var _jump_height : float = 2.5
@export var _jump_dust : PackedScene
var _jump_velocity : float
@onready var _sprite : Sprite2D = $Sprite2D
var gravity = ProjectSettings.get_setting("physics/2d/default_gravity")
var _direction : float
func _ready():
_speed *= Global.ppt
_acceleration *= Global.ppt
_deceleration *= Global.ppt
_jump_height *= Global.ppt
_jump_velocity = sqrt(_jump_height * gravity * 2) * -1
#region Public Methods
func face_left():
_sprite.flip_h = true
func face_right():
_sprite.flip_h = false
func run(direction : float):
_direction = direction
func jump():
if is_on_floor():
velocity.y = _jump_velocity
_spawn_dust(_jump_dust)
func stop_jump():
if velocity.y < 0:
velocity.y = 0
#endregion
func _physics_process(delta : float):
if sign(_direction) == -1:
face_left()
elif sign(_direction) == 1:
face_right()
if is_on_floor():
_ground_physics(delta)
else:
_air_physics(delta)
move_and_slide()
func _ground_physics(delta : float):
#decelerate to zero
if _direction == 0:
velocity.x = move_toward(velocity.x, 0, _deceleration * delta)
#accelerate from not moving or trying to move in same direction
elif velocity.x == 0 || sign(_direction) == sign(velocity.x):
velocity.x = move_toward(velocity.x, _direction * _speed, _acceleration * delta)
#decelerate if trying to move in opposite direction
else:
velocity.x = move_toward(velocity.x, _direction * _speed, _deceleration * delta)
func _air_physics(delta : float):
velocity.y += gravity * delta
if _direction:
velocity.x = move_toward(velocity.x, _direction * _speed, _acceleration * _air_control * delta)
func _spawn_dust(dust : PackedScene):
var _dust = dust.instantiate()
_dust.position = position
_dust.flip_h = _sprite.flip_h
get_parent().add_child(_dust)
Thanks in advance for any insight!