Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Scripts C# for if the player is near the burn object, the player would listen audio source

Discussion in 'Scripting' started by raihan_1, Dec 17, 2015.

  1. raihan_1

    raihan_1

    Joined:
    Dec 17, 2015
    Posts:
    2
    Hello
    i need help for my game. just a scripts that i dont know. so i want to make a scripts that in the game, the player woud listen the audio fire if the player is near the burn object (car). so if the player is not near the object (burn car), the player would not listen the audio but, if the player come again to the object, the player is listen again the audio.
    so please help me to make the script C#. i will apraciete your help. and thank you for you all.

    RAIHAN.
     
  2. Marceta

    Marceta

    Joined:
    Aug 5, 2013
    Posts:
    177
    You can do that with Vector3.Distance.

    What i would do is check distance between player and car and based on distance control volume of burning sound. So if player gets closer it will burn louder and reverse. Of course if you will do that for more objects, things should be done little different.
     
  3. raihan_1

    raihan_1

    Joined:
    Dec 17, 2015
    Posts:
    2
    Can you help to make the scripts ???
     
  4. Marceta

    Marceta

    Joined:
    Aug 5, 2013
    Posts:
    177
    Best way to learn something is to learn from failures. So at least try to do it, then update us here with your code.
     
    Rostam24 likes this.
  5. ArachnidAnimal

    ArachnidAnimal

    Joined:
    Mar 3, 2015
    Posts:
    1,727
    If the audio source is set to 3d, you can just control the volume of the sound based on the distance from the audio listener in the AudioSource setting. This is probably what you want for an audio source such as fire. No code is needed. If you start turning the audio source on and off it wouldn't sound right.

    http://docs.unity3d.com/Manual/class-AudioSource.html
     
    Last edited: Dec 17, 2015
  6. Marceta

    Marceta

    Joined:
    Aug 5, 2013
    Posts:
    177
    That's true, there is actually 3D audio settings overthere for distance, spread, droppler level etc. , but i guess he want some custom control because he asked for code, for example if he wants to reduce sfx based on his own "distance" or whatever is the reason. Also sound wouldn't be just turned off or on, but lerped smoothly between total silence and full sound.

    For example this:
     
    Last edited: Dec 18, 2015