Skip to content

Instantly share code, notes, and snippets.

@Bradshaw
Last active January 29, 2019 10:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Bradshaw/c14da246402ebe3a77bf3248556f5f56 to your computer and use it in GitHub Desktop.
Save Bradshaw/c14da246402ebe3a77bf3248556f5f56 to your computer and use it in GitHub Desktop.
Simple beat follower script demo
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Events;
public class PulseOnBeat : MonoBehaviour {
public float minScale;
public float maxScale;
public float BPM = 140;
float last = Mathf.Infinity;
public UnityEvent uEvent;
public float MPB {
get {
return 1/BPM;
}
}
public float SPB {
get {
return MPB * 60;
}
}
public float seconds {
get {
if (aSource!=null){
return aSource.time;
} else {
return Time.time;
}
}
}
public AudioSource aSource;
// Update is called once per frame
void Update () {
Debug.Log(MPB);
Debug.Log(SPB);
float beat = (seconds / SPB)%1;
float quality = Mathf.Pow((beat-0.5f)*2,2);
transform.localScale = Vector3.one*Mathf.Lerp(minScale, maxScale, beat);
if (beat<last){
uEvent.Invoke();
}
last = beat;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment