ProjectSquareBall/Assets/Scripts/ProtoTyping/AlgorithmTest.cs
2024-09-04 18:39:41 +05:30

45 lines
1.1 KiB
C#

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class AlgorithmTest : MonoBehaviour
{
public List<float> allHits = new List<float>();
public LineRenderer line;
public Vector2 offsetMultipliers = Vector2.one;
void OnDrawGizmos(){
RegenerateLines();
}
void RegenerateLines(){
Vector3[] points = new Vector3[allHits.Count];
for(int i=0; i < points.Length; i++){
points[i] = GetPosition(i);
}
line.positionCount = points.Length;
line.SetPositions(points);
}
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) {
diff = allHits[index] - allHits[index - 1];
prevY = GetPosition(index - 1).y; // Recursively get the previous Y value
}
float flipSide = index % 2 == 0 ? 1 : -1;
float y = prevY + (flipSide * diff);
return new Vector3(hitX * offsetMultipliers.x, y, 0);
}
}