Hi there,
Im trying to make an object pulse to the beat of a soundtrack. The effect I want to achieve is something like the centre hexagon of this game: http://distractionware.com/games/flash/hexagon/.
I already implemented the solution given on this thread, but here the movement is too continuous. I need to have my wave more discreet, like a fast peak and then back to normal.
How can I achieve such an effect?
Thank you!
3 Answers
3It is fairly easy and you can make remarkable stuff with the function GetSpectrumData in the AudioSource-class. For instance: http://youtu.be/zvciEKEjuXI
Have a look at aldonaletto’s answer for more details on how to attack this with code.
Here is my code copy / paste it its complicated ![]()
PulsingObject.cs
using System.Collections;
using UnityEngine;
[RequireComponent(typeof (AudioSource))]
public class PulsingObject : MonoBehaviour
{
AudioSource _audioSource;
public static float[] _samples = new float[512];
public static float[] _freqBand = new float[8];
// Start is called before the first frame update
void Start()
{
_audioSource = GetComponent<AudioSource>();
}
// Update is called once per frame
void Update()
{
GetSpectrumData();
MakeFrequencyBands();
}
void GetSpectrumData()
{
_audioSource.GetSpectrumData(_samples, 0, FFTWindow.Blackman);
}
void MakeFrequencyBands()
{
int count = 0;
for (int i = 0; i < 8; i++)
{
float average = 0;
int sampleCount = (int)Mathf.Pow(2, i) * 2;
if (i == 7)
{
sampleCount += 2;
}
for (int j = 0; j < sampleCount; j++)
{
average += _samples[count] * (count + 1);
count++;
}
average /= count;
_freqBand _= average * 10;_
}
}
}
Place this script on an empty object called Audio.
_____________________________________________________________________________________
PResize.cs
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PResize : MonoBehaviour
{
public int _band;
public float _startScale, _scaleMultiplier;
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
transform.localScale = new Vector3((PulsingObject._freqBand[_band] * _scaleMultiplier) + _startScale, (PulsingObject._freqBand[_band] * _scaleMultiplier)+ _startScale, transform.localScale.z);
}
}
Place this script on any objects you want to pulse.
Thanks -Mika.
Uhm, this look quite advanced to me (I'm a bit of a noob) :( I wonder if there is a way to implement a pulse effect starting from the code I linked before (so without a real spectrum analysis, just setting a bpm value and make my object bounce to the BPM).
– kurai@kurai: Comments! Use comments. This is not an answer to your question.
– Bunny83