Hey people,
I’ve been trying to code a mechanic where you can freely control a fishhook. I want some sort of string to follow the hook, but also allow you to reel it back in. This last past has given me some trouble.
I used a rewind script from Brackeys to reel in the hook on the path you previously moved:
as you can see, everything works except gradually removing the fishline. I achieved the trail effect by instantiating points and adding them to a list, connecting neighboring points with a LineRenderer.
Might be a big question, but does anyone have any tips or ideas on how I could achieve this? I think I’m relatively okay with coding, but nowhere near being a professional.
Do you want the string to exhibit some sort of physics properties? Typically the fishing line is just a straight or curved line from the fishing rod to the hook.
Not necessarily. It’s in a zero gravity environment, so I want the string to kinda float like that. Physics would be interesting but I mostly care about achieving the effect I mentioned for the gameplay to work!
I didn’t quite understand the question in the thread. If you can create a line by casting the hook, why can’t you shorten it in the same way when winding the line back?
It would be even better if you could explain clearly what you want to achieve. How should the line be wound back? Is it simply by moving the hook in the opposite direction of the current path taken?
So you want the line to be erased as you pull the hook back in? Can’t you remove points from the line renderer in the same fashion that you are are adding them?
Yes, I want the line to move back the exact opposite direction like shown in the gif. This in itself works, but removing the line points gradually has given me some trouble. I tried doing it with colliders on the points and the hook, but this will remove any point it has contact with, not just the newest one in the list.
I add the points using a coroutine to place one every millisecond. I tried removing them gradually at the same speed when reeling in, but the rewinding time seemed to just be a tiny bit off (which idk how to fix).
Thanks for clarifying, at first I thought you wanted to simulate winding a fishing line with some more realistic effect, like this (there’s no physics here, just manipulation of an array of points).
I think you don’t necessarily need to set each point in time, but rather at certain distances from each other. As you move the anchor, check the distance to the last point left; if this distance is greater than some value, then add a new point. In other words, it’s like regular ‘checkpointing’. That’s a rough description.
You should definitely give it a try; it would be good brain exercise. Besides, your algorithm might turn out to be much easier to comprehend and more optimal in terms of performance.
I can demonstrate my method, which deals with ‘reeling in the line’. This method takes a list of points and the magnitude by which you need to ‘reel in’. The line doesn’t necessarily have to consist of evenly spaced points; it can be any set, even if it resembles a collection of straight lines.
CODE
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;
}
}
Thank you! I’ll take a look at it. I’ve managed to find a way to do my original plan through the points over distance like you mentioned, but the code you showed could be an interesting different take.