- Home /
Can't crossfade quickly between two audio tracks?
I'm working on my first platformer, (a retro pixel art sidescroller) and I'm trying to follow the great design philosophy of Portal 2, which had short audio loops fade in as the player jumped and soared through the air, and fade out again as they landed. I've got two audio tracks, one of which is a quiet and subdued one, the other one being more catchy and bombastic, to aurally reward the player for jumping and platforming.
Both of them sound bad at the same time though, so I need the more subdued one to play automatically at the start of the level (which I can do by ticking 'Play on Awake') and when the player jumps fade in second track and fade out the first track, and then when the player lands fade out the second track and bring back the first track.
I'm pretty sure that I need both of them to play in the background simultaneously to keep them in sync, one with no volume and one with high volume, so I've already set up two audio Snapshots, one called 'Static' for when the player is not jumping, and one called 'Active' for when the player is running.
I've already got what I believe is the right part of the beginning of the script, using the UnityEngine.Audio and calling out the two Snapshots, as well as setting up a transitional time, but I don't know how to create a crossfade based on when the character jumps, which then fades back when they land. I believe that I need to use a 'vSpeed' (or vertical speed) float, which I've attached to my character to figure out when the transitions would play, but after trawling the forums, I've not been able to find anyone else trying to accomplish this very specific behaviour.
Any guidance would be much appreciated! Cheers, and the script I've got so far is below!
using UnityEngine; using System.Collections; using UnityEngine.Audio;
public class MusicTransition : MonoBehaviour
{
public AudioMixerSnapshop ActiveSnapshot;
public AudioMixerSnapshop StaticSnapshop;
public float TransitionTime = 0.5f;
No, sorry. I'm new to Unity, so I made sure to re-look over the tutorials supplied for basic coding, audio, and snapshots, as well as other people's projects, but I couldn't find anything to help me out.