Changing music when you go to different areas

i am stuck on how to do this
i want the music in my game to change when i go to a different area (in the same scene),for example i want the starting area music to change to different music when my player moves to my hell area

i was never really good at music-related things in unity,could someone help me with this?

Whatever AudioSource is playing your music, you can assign a new AudioClip to its “clip” field and then call Play() on it to start playing the new song.

You may want to fade out the old song first by gradually decreasing its volume.

1 Like

You could use a surround-like system with large falloff at a distance so the sound can not be heard at the location.
AudioSources have that capability, though i dont exactly know their working.

Alternatively you could place the sound source on an object and add a trigger to it, start and stop playing the sound on enter and exit. or onTriggerStay works too but i dont like using that personally.

Alternatively alternatively you can use a distance calculation between the player and the source of the sound, which would work similar to the surround sound, however I would advise against this approach.

1 Like

Issue is that i don’t have a clue how to do that,i don’t know the proper code/script for playing/looping/stopping music

The moving around different areas isn’t an audio related issue. It is important to break down problems into separate issues or you will have difficulty wrapping your head around it.

The playing and changing music issue comes down to using the AudioSource component and assigning AudioClips. You’ll probably want an array or list of AudioClips, which you use to be assigned to the AudioSource. After that it is just using Play and adjusting volume of the AudioSource you need to deal with.

Separately you have the problem of determining what zone of the map your are in (this is a completely separate issue from playing music, treat it as such even though you will eventually use this information to choose the correct music). I’d suggest coming up with a system of zone borders, where you cross the border you enter a new zone using trigger colliders. You could also just use the player’s world position and do it all in script.

When a new zone is entered you call some method that does everything you need to happen when transitioning to a new zone. That can be changing music, changing lighting, async loading a new terrain scene, unloading previous areas of the map, changing post processing effects, etc. Right now of course you’ll just change music, but you’ll likely have other ideas of things you also want to change.

2 Likes

i already have all my audioclips on all 3 audio sources i have,i still don’t get what you mean,what do you mean “Assign” them to the audio source?

Thanks but the script doesn’t quite work how i need it to be.

https://docs.unity3d.com/ScriptReference/AudioSource-clip.html

So in code you set AudioSource.clip to whatever AudioClip you want to play. See the above example at the link.

1 Like

Alright,it works,but the music plays all at once,was i supposed to assign it to all 3 of them or do anything to change the script?

You assign whichever music you want to play to the AudioSource and use Play. If you’re using 3 AudioSources instead of 1 then you’ll need to manage them separately.

1 Like

How do i do that?

i can’t find a single bit of code or way to do that at the moment

Did you look at the manual page that I linked? It shows you the functions that set and play audio clips for an audio source.

In the docs, you can see that the AudioSource has a property called “clip”. That is the audio clip that the AudioSource will play. Set the “clip” to what ever your music’s audio clip is. Notice also there is a method called “Play”. Obviously that’s what you use to play the clip once you’ve assigned it.

2 Likes

Hey,

i know this post might be a little old and maybe I’m just a little late. But hopefully someone will ever need this.
I wrote two scripts that achive exactly this.

#1 The Manager itself.
It needs an Audiosource and this script attached.

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

public class MusicRequester : MonoBehaviour
{
    [SerializeField] private AudioClip areaMusic = default;
    [SerializeField] private Collider2D areaMusicCollider = default;


    private void OnTriggerEnter2D(Collider2D other)
    {
        Character player = other.GetComponent<PlayerMovement>();
        if (player != null)
        {
            MusicManager.RequestMusic(areaMusic);
        }
    }

}

#2 The MusicRequester
It needs an 2D Colliders (could be rewritten to 3D i guess) and the preferred audiclip(music)

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

public class MusicRequester : MonoBehaviour
{
    [SerializeField] private AudioClip areaMusic = default;
    [SerializeField] private Collider2D areaMusicCollider = default;


    private void OnTriggerEnter2D(Collider2D other)
    {
        Character player = other.GetComponent<PlayerMovement>();
        if (player != null)
        {
            MusicManager.RequestMusic(areaMusic);
        }
    }

}

BR

Just use FMOD Studio or something, adaptive audio stuff are much simpler to do there.

Hey, love that you shared this but it looks like you accidentally pasted the code for just the requester twice, wondering if you could share the manager?