Search Unity

  1. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice
  2. Unity is excited to announce that we will be collaborating with TheXPlace for a summer game jam from June 13 - June 19. Learn more.
    Dismiss Notice
  3. Dismiss Notice

Discussion Coding a 2D extendable and retractable fishline

Discussion in 'Scripting' started by SuperGoo, Apr 26, 2024.

  1. SuperGoo

    SuperGoo

    Joined:
    Jul 31, 2021
    Posts:
    14
    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:


    moving fishhook.gif

    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.
     
  2. dstears

    dstears

    Joined:
    Sep 6, 2021
    Posts:
    219
    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.
     
  3. SuperGoo

    SuperGoo

    Joined:
    Jul 31, 2021
    Posts:
    14
    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!
     
  4. samana1407

    samana1407

    Joined:
    Aug 23, 2015
    Posts:
    282
    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?
     
  5. dstears

    dstears

    Joined:
    Sep 6, 2021
    Posts:
    219
    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?
     
    karliss_coldwild likes this.
  6. mgear

    mgear

    Joined:
    Aug 3, 2010
    Posts:
    9,549
  7. SuperGoo

    SuperGoo

    Joined:
    Jul 31, 2021
    Posts:
    14
    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.
     
  8. SuperGoo

    SuperGoo

    Joined:
    Jul 31, 2021
    Posts:
    14
    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).
     
  9. samana1407

    samana1407

    Joined:
    Aug 23, 2015
    Posts:
    282
    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).

     
    Last edited: Apr 28, 2024
    mgear likes this.
  10. SuperGoo

    SuperGoo

    Joined:
    Jul 31, 2021
    Posts:
    14
    Damn that does like really sick tho. Any chance I could actually somehow replicate that with my coding skills? :p
     
  11. samana1407

    samana1407

    Joined:
    Aug 23, 2015
    Posts:
    282
    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 (CSharp):
    1. private void ReelBack(List<Vector3> points, float reelValue)
    2. {
    3.     if (reelValue <= 0)
    4.         return;
    5.  
    6.     Vector3 origin = points[0];
    7.     float reel = reelValue;
    8.  
    9.     // find cut point
    10.     int removePointsFromStart = 0;
    11.  
    12.     Vector3 cutPoint = points[0];
    13.  
    14.     for (int i = 1; i < points.Count; i++)
    15.     {
    16.         removePointsFromStart++;
    17.  
    18.         var current = points[i];
    19.  
    20.         var distPrevToCurrent = (current - cutPoint).magnitude;
    21.  
    22.         if (reel >= distPrevToCurrent)
    23.         {
    24.             reel -= distPrevToCurrent;
    25.             cutPoint = current;
    26.         }
    27.         else
    28.         {
    29.             cutPoint = Vector3.Lerp(cutPoint, current, reel / distPrevToCurrent);
    30.             break;
    31.         }
    32.     }
    33.  
    34.     points.RemoveRange(0, removePointsFromStart - 1);
    35.     points[0] = cutPoint;
    36.  
    37.  
    38.     // reel
    39.  
    40.     float dist = (points[0] - points[1]).magnitude;
    41.  
    42.     points[0] = origin;
    43.     float nextDist = 0;
    44.  
    45.     for (int i = 1; i < points.Count; i++)
    46.     {
    47.         if (i < points.Count - 1)
    48.         {
    49.             nextDist = (points[i] - points[i + 1]).magnitude;
    50.         }
    51.  
    52.         points[i] = points[i - 1] + (points[i] - points[i - 1]).normalized * dist;
    53.         dist = nextDist;
    54.     }
    55.  
    56. }

     
  12. SuperGoo

    SuperGoo

    Joined:
    Jul 31, 2021
    Posts:
    14
    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.