Bouncing effect + music sync
This commit is contained in:
@@ -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