Play Audio Script - Volume adjusting

Started by
2 comments, last by mbmusicpl 6 years, 7 months ago

Hi,

I am composer and sound designer that is trying to learn some codeing :) Could You help me and tell me what should i add to change volume to 1.0F when enter trigger

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class AudioPlay : MonoBehaviour {


    public AudioClip strings_1;
    AudioSource audioSource;


    // Use this for initialization
    void Start () {

        audioSource = GetComponent<AudioSource> (); 

        audioSource.PlayOneShot (strings_1, 0.0F );   - when game starts i want it to start but with 0 volume

    }

    // Update is called once per frame
    void Update () {

    }

    void OnTriggerEnter2D (Collider2D other) 
    {

        if(other.gameObject.tag == "slotviolin")

        {
            Debug.Log ("coilder works");
    
        // how can i change volume to 1.0F and if it's possible how to make it smooth ?


        }
    }
}

 

Thanks :)

Advertisement

You should look at Unity's manual.

Check out AudioSource: https://docs.unity3d.com/ScriptReference/AudioSource.html

On Unity's site they have a tutorials section for Audio: https://unity3d.com/learn/tutorials/topics/audio

I'm not 100% sure about fading as I don't use Unity, but check out this script for smooth audio transitions: http://wiki.unity3d.com/index.php/Fading_Audio_Source

This should help solve your problem.

 

Programmer and 3D Artist

Thx Rutin,

Somehow i have missed this tutorial with the solution :). I have to use mixer snapshots !

I still have a problem with UNITY. I am making 2D game so i changed it on tutorial script:

The problem is that i am working on UI panels and colliders doesn't work normaly here. I thing I have to use EventTrigger Componet 

https://imgur.com/a/hEM59

https://imgur.com/a/YkXuY

but what next ???

using UnityEngine;
using System.Collections;
using UnityEngine.Audio;

public class InOutMusic : MonoBehaviour {


    public AudioMixerSnapshot OutSlot;
    public AudioMixerSnapshot InSlot;
    public AudioClip[] stings;
    public AudioSource stingSource;
    public float bpm = 128;


    private float m_TransitionIn;
    private float m_TransitionOut;
    private float m_QuarterNote;

    // Use this for initialization
    void Start () 
    {
        m_QuarterNote = 60 / bpm;
        m_TransitionIn = m_QuarterNote;
        m_TransitionOut = m_QuarterNote * 32;

    }

    void OnTriggerEnter2D (Collider2D other) - i have added 2d
    {
        if (other.CompareTag("slotviolin"))
        {
            
            InSlot.TransitionTo(m_TransitionIn);

        }
    }

    void OnTriggerExit2D(Collider2D other) - i have added 2d
    {
        if (other.CompareTag("slotviolin"))
        {
            OutSlot.TransitionTo(m_TransitionOut);
        }
    }

}

 

 

This topic is closed to new replies.

Advertisement