1. Hey! Guest! The 36th GMC Jam will take place between February 27th, 12:00 UTC - March 2nd, 12:00 UTC. Why not join in! Click here to find out more!
    Dismiss Notice

GM:S 1.4 Help with 3D sounds?

Discussion in 'Programming' started by Zachary_Alan, Aug 5, 2019.

Tags:
  1. Zachary_Alan

    Zachary_Alan Member

    Joined:
    Dec 1, 2018
    Posts:
    38
    Hi, I want to make it so that sounds like car engines and gun shots in my top down game can only be heard from a certain distance away, and have them fade out as the object making the sound gets farther away. I've been watching videos and reading things, but I don't get it. Maybe they're not very good tutorials, or maybe I'm just slow. Either way I can't find a clear and simple way for my little brain to understand how to do it.

    I don't want to sound like someone who can't be bothered to learn on their own but I literally can't do this ****.
     
  2. OblivionSkull21

    OblivionSkull21 Member

    Joined:
    Sep 26, 2017
    Posts:
    318
    https://docs2.yoyogames.com/source/_build/3_scripting/4_gml_reference/audio/audio_play_sound_at.html

    audio_play_sound_at is what you're looking for.

    It lets you pick where in the room to play the sound and how far away the sound reaches. If the player is outside of this range, they won't hear the sound, and it will get louder as they get closer to the source.

    Ex:
    Code:
    audio_play_sound_at(snd_Waterfall, x, y, 0, 100, 300, 1, true, 1);
    The sound, waterfall, will play at the x and y position of the calling instance and 0 on the z axis. If I'm not mistaken, the 100 is the distance away from the source that the sound should start to fade, and 300 is how far away until the sound has completely faded. 1 is the falloff factor, which should be changed based on what sound model you set with audio_falloff_set_model, but the default of 1 is usually fine. https://docs2.yoyogames.com/source/..._reference/audio/audio_falloff_set_model.html

    True makes sure the sound loops, and 1 is the priority.
     
    Dr_Nomz and KPJ like this.
  3. Zachary_Alan

    Zachary_Alan Member

    Joined:
    Dec 1, 2018
    Posts:
    38
    That's it?????????? I don't need to do the emitter and listener crap?
     
  4. OblivionSkull21

    OblivionSkull21 Member

    Joined:
    Sep 26, 2017
    Posts:
    318
    Listeners/emitters are for more complex sound. I think that if you just want a sound to play at a spot and fade as it gets farther from the player, then audio_play_sound_at is all you need.
     
    KPJ likes this.
  5. Dr_Nomz

    Dr_Nomz Member

    Joined:
    Oct 31, 2016
    Posts:
    644
    Funny, I was just about to ask about this lol.

    OblivionSkull, is 1 just for linear sound, like the chart on that link you posted?
     
  6. OblivionSkull21

    OblivionSkull21 Member

    Joined:
    Sep 26, 2017
    Posts:
    318
    I think so, I'm not entirely sure as I've never changed the falloff factor from its default.

    "falloff factor - The falloff factor is used in distance attenuation based on the inverse distance model and sets the final minimum threshold for a sound with falloff."

    I'm assuming if you have falloff factor of 2, then the the falloff will be twice the normal size, but I could be wrong.
     
  7. Zachary_Alan

    Zachary_Alan Member

    Joined:
    Dec 1, 2018
    Posts:
    38
    It's not working. I want it for the sirens on the police cars but no sound is playing.
    Code:
    if(distance_to_object(obj_player) < 300){
            mySiren = audio_play_sound_at( snd_siren1 , x , y , 0 , 100 , 300 , 1 , true , 1 );
        }
    I don't know what I did, the code seems simple. I read the article you linked, and put this code in a step event in the player object:
    Code:
    audio_listener_position( x , y , 0 );
    audio_listener_orientation(0, 0, 1000, 0, -1, 0);
     
  8. OblivionSkull21

    OblivionSkull21 Member

    Joined:
    Sep 26, 2017
    Posts:
    318
    First, you don't need the distance to object part. The sound is going to be playing at the coordinates of the car whether the player is within range or not. It will always be playing. However, you won't be able to hear it unless you are within 300.

    The audio_listener_position is correct.

    I'm not super familiar with audio listeners myself, I think I had to post a thread about it a while back. In my game, I have

    Code:
    audio_listener_orientation(0,-1,0,0,0,-1);
    That seems to work for me.
     
  9. Zachary_Alan

    Zachary_Alan Member

    Joined:
    Dec 1, 2018
    Posts:
    38
    Now, the sound isn't following the cop cars, and I can still hear the sound even when they are over 300 pixels away.
     
    Last edited: Aug 5, 2019
  10. OblivionSkull21

    OblivionSkull21 Member

    Joined:
    Sep 26, 2017
    Posts:
    318
    Try something like this.

    In the car's create event:
    Code:
    audio_falloff_set_model(audio_falloff_linear_distance); //standard linear model
    In the car's step event:
    Code:
    with(obj_player){
    audio_listener_position(x, y, 0); //position listener at player's coordinates
    audio_listener_orientation(0,-1,0,0,0,-1);
    }
    
    audio_play_sound_at(snd_siren1, x, y, 0, 100, 300, 1, true, 1); //play the sound at the car's coordinates
    
    If that doesn't work, check to see if you've set the actual sound's output to 3D. I read some where that you should do this, but I haven't done this with my game and my sounds work just fine, so idk.

    Also, if you're only wanting one sound to play for the group of cars instead of six sounds for six cars, then you should check to make sure the sound isn't already playing.
    Code:
    if !audio_is_playing(snd_siren1){
    audio_play_sound_at(snd_siren1, x, y, 0, 100, 300, 1, true, 1); //play the sound at the car's coordinates
    }
    If all else fails, try this tutorial. Though, it does use emitters, which I'm not familiar with. Regardless, the code I provided works in my game so there shouldn't be any reason for it not to in yours.
     
  11. Zachary_Alan

    Zachary_Alan Member

    Joined:
    Dec 1, 2018
    Posts:
    38
    I just learned how to use emitters it wasn't as hard as I first thought.
     
    OblivionSkull21 likes this.

Share This Page