Bouncing effect + music sync
This commit is contained in:
124
Assets/Scripts/EasingFunctions.cs
Normal file
124
Assets/Scripts/EasingFunctions.cs
Normal file
@@ -0,0 +1,124 @@
|
||||
using System;
|
||||
|
||||
namespace Kryz.Tweening
|
||||
{
|
||||
// Made with the help of this great post: https://joshondesign.com/2013/03/01/improvedEasingEquations
|
||||
|
||||
// --------------------------------- Other Related Links --------------------------------------------------------------------
|
||||
// Original equations, bad formulation: https://github.com/danro/jquery-easing/blob/master/jquery.easing.js
|
||||
// A few equations, very simplified: https://gist.github.com/gre/1650294
|
||||
// Easings.net equations, simplified: https://github.com/ai/easings.net/blob/master/src/easings/easingsFunctions.ts
|
||||
|
||||
public static class EasingFunctions
|
||||
{
|
||||
public static float Linear(float t) => t;
|
||||
|
||||
public static float InQuad(float t) => t * t;
|
||||
public static float OutQuad(float t) => 1 - InQuad(1 - t);
|
||||
public static float InOutQuad(float t)
|
||||
{
|
||||
if (t < 0.5) return InQuad(t * 2) / 2;
|
||||
return 1 - InQuad((1 - t) * 2) / 2;
|
||||
}
|
||||
|
||||
public static float InCubic(float t) => t * t * t;
|
||||
public static float OutCubic(float t) => 1 - InCubic(1 - t);
|
||||
public static float InOutCubic(float t)
|
||||
{
|
||||
if (t < 0.5) return InCubic(t * 2) / 2;
|
||||
return 1 - InCubic((1 - t) * 2) / 2;
|
||||
}
|
||||
|
||||
public static float InQuart(float t) => t * t * t * t;
|
||||
public static float OutQuart(float t) => 1 - InQuart(1 - t);
|
||||
public static float InOutQuart(float t)
|
||||
{
|
||||
if (t < 0.5) return InQuart(t * 2) / 2;
|
||||
return 1 - InQuart((1 - t) * 2) / 2;
|
||||
}
|
||||
|
||||
public static float InQuint(float t) => t * t * t * t * t;
|
||||
public static float OutQuint(float t) => 1 - InQuint(1 - t);
|
||||
public static float InOutQuint(float t)
|
||||
{
|
||||
if (t < 0.5) return InQuint(t * 2) / 2;
|
||||
return 1 - InQuint((1 - t) * 2) / 2;
|
||||
}
|
||||
|
||||
public static float InSine(float t) => 1 - (float)Math.Cos(t * Math.PI / 2);
|
||||
public static float OutSine(float t) => (float)Math.Sin(t * Math.PI / 2);
|
||||
public static float InOutSine(float t) => (float)(Math.Cos(t * Math.PI) - 1) / -2;
|
||||
|
||||
public static float InExpo(float t) => (float)Math.Pow(2, 10 * (t - 1));
|
||||
public static float OutExpo(float t) => 1 - InExpo(1 - t);
|
||||
public static float InOutExpo(float t)
|
||||
{
|
||||
if (t < 0.5) return InExpo(t * 2) / 2;
|
||||
return 1 - InExpo((1 - t) * 2) / 2;
|
||||
}
|
||||
|
||||
public static float InCirc(float t) => -((float)Math.Sqrt(1 - t * t) - 1);
|
||||
public static float OutCirc(float t) => 1 - InCirc(1 - t);
|
||||
public static float InOutCirc(float t)
|
||||
{
|
||||
if (t < 0.5) return InCirc(t * 2) / 2;
|
||||
return 1 - InCirc((1 - t) * 2) / 2;
|
||||
}
|
||||
|
||||
public static float InElastic(float t) => 1 - OutElastic(1 - t);
|
||||
public static float OutElastic(float t)
|
||||
{
|
||||
float p = 0.3f;
|
||||
return (float)Math.Pow(2, -10 * t) * (float)Math.Sin((t - p / 4) * (2 * Math.PI) / p) + 1;
|
||||
}
|
||||
public static float InOutElastic(float t)
|
||||
{
|
||||
if (t < 0.5) return InElastic(t * 2) / 2;
|
||||
return 1 - InElastic((1 - t) * 2) / 2;
|
||||
}
|
||||
|
||||
public static float InBack(float t)
|
||||
{
|
||||
float s = 1.70158f;
|
||||
return t * t * ((s + 1) * t - s);
|
||||
}
|
||||
public static float OutBack(float t) => 1 - InBack(1 - t);
|
||||
public static float InOutBack(float t)
|
||||
{
|
||||
if (t < 0.5) return InBack(t * 2) / 2;
|
||||
return 1 - InBack((1 - t) * 2) / 2;
|
||||
}
|
||||
|
||||
public static float InBounce(float t) => 1 - OutBounce(1 - t);
|
||||
public static float OutBounce(float t)
|
||||
{
|
||||
float div = 2.75f;
|
||||
float mult = 7.5625f;
|
||||
|
||||
if (t < 1 / div)
|
||||
{
|
||||
return mult * t * t;
|
||||
}
|
||||
else if (t < 2 / div)
|
||||
{
|
||||
t -= 1.5f / div;
|
||||
return mult * t * t + 0.75f;
|
||||
}
|
||||
else if (t < 2.5 / div)
|
||||
{
|
||||
t -= 2.25f / div;
|
||||
return mult * t * t + 0.9375f;
|
||||
}
|
||||
else
|
||||
{
|
||||
t -= 2.625f / div;
|
||||
return mult * t * t + 0.984375f;
|
||||
}
|
||||
}
|
||||
public static float InOutBounce(float t)
|
||||
{
|
||||
if (t < 0.5) return InBounce(t * 2) / 2;
|
||||
return 1 - InBounce((1 - t) * 2) / 2;
|
||||
}
|
||||
}
|
||||
}
|
||||
11
Assets/Scripts/EasingFunctions.cs.meta
Normal file
11
Assets/Scripts/EasingFunctions.cs.meta
Normal file
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 762943c0218e5804c878a0ed4c351836
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -1,6 +1,7 @@
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
using UnityEngine.UIElements.Experimental;
|
||||
|
||||
public class AlgorithmTest : MonoBehaviour
|
||||
{
|
||||
@@ -8,7 +9,8 @@ public class AlgorithmTest : MonoBehaviour
|
||||
public LineRenderer line;
|
||||
public Vector2 offsetMultipliers = Vector2.one;
|
||||
public GameObject objectToMove; // The GameObject to move
|
||||
|
||||
public AudioSource source;
|
||||
public float audioOffset = 0.05f;
|
||||
private Vector3[] points;
|
||||
|
||||
|
||||
@@ -16,20 +18,24 @@ public class AlgorithmTest : MonoBehaviour
|
||||
|
||||
|
||||
Coroutine movingCoroutine;
|
||||
void OnDrawGizmos(){
|
||||
void OnDrawGizmos()
|
||||
{
|
||||
RegenerateLines();
|
||||
|
||||
if(MoveCmd){
|
||||
if (MoveCmd)
|
||||
{
|
||||
MoveCmd = false;
|
||||
if(movingCoroutine!= null){StopCoroutine(movingCoroutine);}
|
||||
if (movingCoroutine != null) { StopCoroutine(movingCoroutine); }
|
||||
movingCoroutine = StartCoroutine(MoveAlongPath());
|
||||
}
|
||||
}
|
||||
|
||||
void RegenerateLines(){
|
||||
void RegenerateLines()
|
||||
{
|
||||
points = new Vector3[allHits.Count];
|
||||
|
||||
for (int i = 0; i < points.Length; i++) {
|
||||
for (int i = 0; i < points.Length; i++)
|
||||
{
|
||||
points[i] = GetPosition(i);
|
||||
}
|
||||
|
||||
@@ -37,35 +43,45 @@ public class AlgorithmTest : MonoBehaviour
|
||||
line.SetPositions(points);
|
||||
}
|
||||
|
||||
Vector3 GetPosition(int index) {
|
||||
void Update(){
|
||||
objectToMove.transform.position = GetPositionAtTime(source.time + audioOffset);
|
||||
}
|
||||
|
||||
Vector3 GetPosition(int index)
|
||||
{
|
||||
float hitX = allHits[index];
|
||||
|
||||
float diff = 0;
|
||||
float prevY = 0;
|
||||
|
||||
|
||||
// Calculate diff and prevY if the index is greater than 0
|
||||
if (index > 0) {
|
||||
if (index > 0)
|
||||
{
|
||||
diff = allHits[index] - allHits[index - 1];
|
||||
prevY = points[index - 1].y; // Use the already calculated point to avoid recursion
|
||||
}
|
||||
|
||||
float flipSide = index % 2 == 0 ? 1 : -1;
|
||||
float y = prevY + (flipSide * diff);
|
||||
float y = prevY + (flipSide * diff * offsetMultipliers.y);
|
||||
|
||||
return new Vector3(hitX * offsetMultipliers.x, y, 0);
|
||||
}
|
||||
|
||||
// Call this function to start moving the GameObject along the path
|
||||
public void StartMovement() {
|
||||
if (objectToMove != null && points != null && points.Length > 1) {
|
||||
public void StartMovement()
|
||||
{
|
||||
if (objectToMove != null && points != null && points.Length > 1)
|
||||
{
|
||||
StartCoroutine(MoveAlongPath());
|
||||
}
|
||||
}
|
||||
|
||||
private IEnumerator MoveAlongPath() {
|
||||
float timer=0;
|
||||
while(timer < allHits[allHits.Count-1]){
|
||||
timer +=Time.deltaTime;
|
||||
private IEnumerator MoveAlongPath()
|
||||
{
|
||||
float timer = 0;
|
||||
while (timer < allHits[allHits.Count - 1])
|
||||
{
|
||||
timer += Time.deltaTime;
|
||||
yield return null;
|
||||
objectToMove.transform.position = GetPositionAtTime(timer);
|
||||
}
|
||||
@@ -77,46 +93,42 @@ public class AlgorithmTest : MonoBehaviour
|
||||
// yield return StartCoroutine(MoveFromTo(objectToMove, startPoint, endPoint, duration));
|
||||
// }
|
||||
}
|
||||
|
||||
private IEnumerator MoveFromTo(GameObject obj, Vector3 start, Vector3 end, float duration) {
|
||||
float elapsedTime = 0;
|
||||
|
||||
while (elapsedTime < duration) {
|
||||
obj.transform.position = Vector3.Lerp(start, end, elapsedTime / duration);
|
||||
elapsedTime += Time.deltaTime;
|
||||
yield return null;
|
||||
public float bouncingIntensity = 0.5f;
|
||||
public Vector3 GetPositionAtTime(float time)
|
||||
{
|
||||
if (points == null || points.Length < 2)
|
||||
{
|
||||
return Vector3.zero; // Return zero if points are not set up
|
||||
}
|
||||
|
||||
obj.transform.position = end;
|
||||
}
|
||||
public Vector3 GetPositionAtTime(float time) {
|
||||
if (points == null || points.Length < 2) {
|
||||
return Vector3.zero; // Return zero if points are not set up
|
||||
}
|
||||
|
||||
// If the time is before the first hit, return the first position
|
||||
if (time <= allHits[0]) {
|
||||
return points[0];
|
||||
}
|
||||
|
||||
// If the time is after the last hit, return the last position
|
||||
if (time >= allHits[allHits.Count - 1]) {
|
||||
return points[points.Length - 1];
|
||||
}
|
||||
|
||||
// Find the current segment based on the given time
|
||||
for (int i = 0; i < allHits.Count - 1; i++) {
|
||||
if (time >= allHits[i] && time < allHits[i + 1]) {
|
||||
Vector3 startPoint = points[i];
|
||||
Vector3 endPoint = points[i + 1];
|
||||
float segmentDuration = allHits[i + 1] - allHits[i];
|
||||
float t = (time - allHits[i]) / segmentDuration;
|
||||
|
||||
return Vector3.Lerp(startPoint, endPoint, t);
|
||||
// If the time is before the first hit, return the first position
|
||||
if (time <= allHits[0])
|
||||
{
|
||||
return points[0];
|
||||
}
|
||||
|
||||
// If the time is after the last hit, return the last position
|
||||
if (time >= allHits[allHits.Count - 1])
|
||||
{
|
||||
return points[points.Length - 1];
|
||||
}
|
||||
|
||||
// Find the current segment based on the given time
|
||||
for (int i = 0; i < allHits.Count - 1; i++)
|
||||
{
|
||||
if (time >= allHits[i] && time < allHits[i + 1])
|
||||
{
|
||||
Vector3 startPoint = points[i];
|
||||
Vector3 endPoint = points[i + 1];
|
||||
float segmentDuration = allHits[i + 1] - allHits[i];
|
||||
float t = (time - allHits[i]) / segmentDuration;
|
||||
|
||||
|
||||
return Vector3.Lerp(startPoint, endPoint, Mathf.Pow(t,bouncingIntensity));
|
||||
}
|
||||
}
|
||||
|
||||
return Vector3.zero; // Return zero if no valid segment is found (should not happen)
|
||||
}
|
||||
|
||||
return Vector3.zero; // Return zero if no valid segment is found (should not happen)
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user