Created
April 29, 2024 04:47
-
-
Save unitycoder/486954ba55d1c97823da62d224370e18 to your computer and use it in GitHub Desktop.
Reel Line made from points back
This file contains 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
// https://forum.unity.com/threads/coding-a-2d-extendable-and-retractable-fishline.1586673/#post-9801981 | |
private void ReelBack(List<Vector3> points, float reelValue) | |
{ | |
if (reelValue <= 0) | |
return; | |
Vector3 origin = points[0]; | |
float reel = reelValue; | |
// find cut point | |
int removePointsFromStart = 0; | |
Vector3 cutPoint = points[0]; | |
for (int i = 1; i < points.Count; i++) | |
{ | |
removePointsFromStart++; | |
var current = points[i]; | |
var distPrevToCurrent = (current - cutPoint).magnitude; | |
if (reel >= distPrevToCurrent) | |
{ | |
reel -= distPrevToCurrent; | |
cutPoint = current; | |
} | |
else | |
{ | |
cutPoint = Vector3.Lerp(cutPoint, current, reel / distPrevToCurrent); | |
break; | |
} | |
} | |
points.RemoveRange(0, removePointsFromStart - 1); | |
points[0] = cutPoint; | |
// reel | |
float dist = (points[0] - points[1]).magnitude; | |
points[0] = origin; | |
float nextDist = 0; | |
for (int i = 1; i < points.Count; i++) | |
{ | |
if (i < points.Count - 1) | |
{ | |
nextDist = (points[i] - points[i + 1]).magnitude; | |
} | |
points[i] = points[i - 1] + (points[i] - points[i - 1]).normalized * dist; | |
dist = nextDist; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment