Playing audiostream2d cancels rigidbody2d linear_velocity

Godot Version



I’ve been working through the following udemy flappy bird course which appears to have been written around Godot 3.x.

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

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:
      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
       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