Last active
January 29, 2019 10:12
-
-
Save Bradshaw/c14da246402ebe3a77bf3248556f5f56 to your computer and use it in GitHub Desktop.
Simple beat follower script demo
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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