Playing audiostream2d cancels rigidbody2d linear_velocity

Godot Version

4.2.1

Question

I’ve been working through the following udemy flappy bird course which appears to have been written around Godot 3.x.
https://www.udemy.com/share/103H2m3@fqzYMoVC84cvbbLYOSI8bczaM5wJoBPI9CmzQ_tywXoavg0GLm1V-XfZltcUBeXk/

Everything was working well, until I added an audiostream to play during the flap function:
func flap():
linear_velocity.y = FLAP_FORCE
angular_velocity = -8.0
wing.play()

The sound plays on time and completely, however seems to negate the setting of the linear_velocity.y, and the bird no longer gains height. Angular velocity still functions as expected.

I did see mention of using integrate_forces as a hover tip on the linear_velocity property in the inspector, is this something I should be using instead?

My best guess is calling the audiostreamplayer2d within the flap function introduces a sync/timing issue into the mix?

Please help.

P.S. Apologies for any missing info, it’s my first forum post.
Thanks in advance!!

With rigidbody you will want to use apply_force or apply_impulse

1 Like

Thanks pennyloafers. I ended up resolving the vertical velocity issue when sound was playing, by moving code from _process to _physics_process.
I tried using _integrate_forces with apply_force and then apply_impulse, but the result felt more like a cumulative acceleration where rapid mouse clicking would eject the player up off the top of the screen.
While using linear_velocity seems to be discouraged in the docs, trying to control or maintain the height of a “flappy bird” character with _integrate_forces was extremely difficult.

apply_impulse is preferred for jumping as it bypass the time delta calculation and essentially set an acceleration directly. So take into consideration of your mass and force values.

so a user can spam or hold the input you will need to limit this input. I would separate the input sensing via func _unhandled_input(event). that will set a jump value to true and in the physics loop I would then check the jump value apply a single force application and set it to false.

var should_jump:bool = false
func _unhandled_input(event):
  if event.is_action("my_jump_action"):
    if event.is_pressed():
      should_jump = true

func _phyisics_process(_delta):
   if should_jump:
      apply_central_impulse(some_jump_force):
      should_jump == false

OR if you want a hold related jump.

const J_FRAMES_INIT:int = 30
var jump_frames:int = J_FRAMES_INIT 
var should_jump:bool = false
func _unhandled_input(event):
  if event.is_action("my_jump_action"):
    if event.is_pressed():
      should_jump = true
      jump_frames = J_FRAMES_INIT
    else:
       should_jump = false

func _phyisics_process(_delta):
   if should_jump and jump_frames > 0:
      apply_central_force(some_smaller_jump_force * jump_frames / J_FRAMES_INIT) # diminishing force the longer the jump is held.
      jump_frames -= 1