Home > Enterprise >  Unity audio perameters when picked up item?
Unity audio perameters when picked up item?

Time:05-18

So I'm creating a game in unity and I want the music to change when the player picks up different items eg a coin, bread, etc. But I'm not sure where I would get started with that does anyone have links or knowledge on how I can do this? Also I am using FMOD but don't want to use FMOD to do this

CodePudding user response:

Maybe you can use the component, called Box Collider.

  1. Unity->Help->Script Reference, open API documentation.
  2. Search: collider, then find a function, named OnTriggerEnter.
  3. Then follow the example.

How to open API

Search and find

OnTriggerEnter

CodePudding user response:

Attach a script containing this to the AudioSource that plays the music. Make sure that loop is unchecked on the AudioSource

    public AudioClip normalClip;//the music to play normally
    public AudioClip specialClip;//the music to play when an item is obtained
    AudioSource audioSource;//the music player

    void Start()
    {
        audioSource = GetComponent<AudioSource>();//find the music player on this GameObject
    }

    void Update()
    {
        //if music player finished playing, start next loop of normal music
        if (!audioSource.isPlaying)
        {
            audioSource.clip = normalClip;
            audioSource.Play();
        }
    }
    
    //starts a loop of the item-obtained music
    void OnObtainedItem(){
        audioSource.clip = specialClip;
        audioSource.Play();
    }

When you collect an item, call OnObtainedItem();

  • Related