• Advertisement

Play Audio Script - Volume adjusting

Recommended Posts

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

Edited by mbmusicpl

Share this post


Link to post
Share on other sites
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.

 

Edited by Rutin

Share this post


Link to post
Share on other sites

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);
        }
    }

}

 

 

Edited by mbmusicpl

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now


  • Advertisement