Looking for some guidance on how to integrate Reactional Music to enable dynamic lighting that syncs with music beats in a game. Any tutorial or example you can provide would be greatly appreciated! Thank you!
Hi @carrotwho and welcome to the forum! I will be looking to add more tutorials like this in the near future, but in the meantime I hope this answer will be helpful.
With Reactional there are several ways to achieve this. Below is a script I made for a demo scene recently. In that scene I was using the high definition render pipeline, but the same principle can be applied to any light with some modification.
Code Example
using UnityEngine;
using UnityEngine.Rendering.HighDefinition;
public class LightIntensityBeat : MonoBehaviour
{
public HDAdditionalLightData light;
public AnimationCurve animationCurve;
public float maxIntensity = 30000f;
public float beatFrequency = 2f;
public bool useLux = false;
public float offset = 0f;
void Start()
{
light = GetComponent<HDAdditionalLightData>();
}
void Update()
{
if(light == null)
return;
var value = animationCurve.Evaluate(((ReactionalEngine.Instance.CurrentBeat + offset) % beatFrequency)/beatFrequency);
if(useLux)
light.SetIntensity(value * maxIntensity, LightUnit.Lux);
else
light.SetIntensity(value * maxIntensity, LightUnit.Lumen);
}
}
Note that I am using ReactionalEngine.Instance.CurrentBeat
however the “proper” approach is Reactional.Playback.MusicSystem.GetCurrentBeat()
but the second simply returns the first so I tend to use them interchangeably.
Inspector settings
With this setup you will, of course need to reference the light source itself.
Then I have set up an animation curve going from 1 to 0.
The animation curve is then evaluated by the beat clock using the CurrentBeat.
The MaxIntensity variable is basically multiplying the 1 to 0 range into something that makes sense for the light.
BeatFrequency is how often it should happen; in this case every 2 beats.
The UseLux boolean is specific to this use case, as some of my lights in the scene uses Lux and some uses Lumen for it’s intensity.
The offset is a beat offset in case you want to delay the light ever so slightly, or have it trigger slightly early; If used I normally put something like 0.1 or so at most.
Example
This script will yield something like this!
I hope that helps!