Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Audio Audio Question

Discussion in 'Audio & Video' started by Bainto93, Jun 12, 2018.

  1. Bainto93

    Bainto93

    Joined:
    Mar 27, 2016
    Posts:
    15
    Ok.. so let me say im new to both unity and scripting as a whole... Ive been working with it for about a month or so and creating a new game. Currently im setting up some sounds and I have an ambient sound set as 3D at the start of the game. However, I want it setup to drop that upon entering a building and instead start playing another ambient sound and loop it till the player exits and then fade and pick back up the other ambient. I looked for about 30 min for some thoughts on starting it and advice. I made one for a trigger that plays once and then is set to played = true and wont play anymore but I want it to overlay the ambient sound.


    here's the one i created and used a few times already...

    using UnityEngine;
    using System.Collections;
    public class PlaySound : MonoBehaviour
    {
    public AudioClip SoundToPlay;
    public float Volume;
    AudioSource audio;
    public bool alreadyPlayed = false;
    void Start()
    {
    audio = GetComponent<AudioSource>();
    }
    void OnTriggerEnter()
    {
    if (!alreadyPlayed)
    {
    audio.PlayOneShot(SoundToPlay, Volume);
    alreadyPlayed = true;
    }
    }
    }
     
  2. Docaroo

    Docaroo

    Joined:
    Nov 7, 2017
    Posts:
    82
    I would just do this by modifying the volume of the first ambient sound to 0 (could even lerp and fade it down to 0) when the player is in the volume and then it brings the volume back up again when the player leaves that volume?
     
  3. Bainto93

    Bainto93

    Joined:
    Mar 27, 2016
    Posts:
    15
    I fixed this by instead making it a trigger enter using my other question and removing the need for this code, then integrating it with playmaker and setting up different music based on the location of the player in the map :p ty