Search Unity

Question Generate points along a line of a certain distance between points

Discussion in 'Scripting' started by TheSorrowRaven, Apr 13, 2021.

  1. TheSorrowRaven

    TheSorrowRaven

    Joined:
    Dec 18, 2017
    Posts:
    6
    Hi, I'm trying to use C# to generate points along a path/line in a line renderer.

    Based on this image, the black line is my line renderer path, red color are all the same distance (I can define) and blue dots are the points I need. I think I can calculate a straight line easily to reach my desired effect, but I have no idea how to program the curved part?

    Note that the blue dots at the curve are the same distance across the path of the line, NOT distance between the two (not direct distance between the two points).

    How could I achieve this? I make do with pseudo codes, but C# code is definitely appreciated.
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,751
    I don't have direct code but basically you would keep a float that indicated distance, and then consider each defined gap in your LineRenderer points, adding that gap size to the distance.

    When distance exceeds the blue interval you want, then emplace a blue dot as a fraction of how much along that LineRenderer gap it should be, then subtract the blue dot gap from your distance and continue.

    To get it working, make a simple 3-point line renderer so you can easily test your code and print out each quantity as you iterate using Debug.Log()
     
  3. TheSorrowRaven

    TheSorrowRaven

    Joined:
    Dec 18, 2017
    Posts:
    6

    I've successfully done it! Thanks Kurt!

    Here's the code if anyone needs it
    Code (CSharp):
    1.  
    2.     public float pointDistance;
    3.  
    4.     public void MarkPoint(Vector3 position)
    5.     {
    6.         Vector3 target = transform.position + position;
    7.         Vector3 left, right, up, down;
    8.         left = right = up = down = target;
    9.         left.x -= .5f;
    10.         right.x += .5f;
    11.         up.y += .5f;
    12.         down.y -= .5f;
    13.         Debug.DrawLine(left, up, Color.yellow, 10);
    14.         Debug.DrawLine(up, right, Color.yellow, 10);
    15.         Debug.DrawLine(right, down, Color.yellow, 10);
    16.         Debug.DrawLine(down, left, Color.yellow, 10);
    17.     }
    18.  
    19.     [ContextMenu("Generate Points")]
    20.     public void GeneratePoints()
    21.     {
    22.         if (lineRenderer.positionCount <= 1)
    23.         {
    24.             return;
    25.         }
    26.         Vector3 start = lineRenderer.GetPosition(0);
    27.         Vector3 next = lineRenderer.GetPosition(1);
    28.         MarkPoint(start);
    29.         float distance = 0;
    30.         float remainingDistance = 0;
    31.         int size = lineRenderer.positionCount;
    32.         int i = 1;
    33.         int j = 0;
    34.         while (true)
    35.         {
    36.             j++;
    37.             if (Step2Points(start, next, ref distance, ref remainingDistance, out Vector3 point))
    38.             {
    39.                 MarkPoint(point);
    40.                 start = point;
    41.             }
    42.             else
    43.             {
    44.                 i++;
    45.                 if (i == size)
    46.                 {
    47.                     break;
    48.                 }
    49.                 start = next;
    50.                 next = lineRenderer.GetPosition(i);
    51.             }
    52.             if (j == 10000)
    53.             {
    54.                 Debug.Log("10000 force stop");
    55.                 return;
    56.             }
    57.         }
    58.  
    59.     }
    60.  
    61.     public bool Step2Points(Vector3 start, Vector3 next, ref float distance, ref float remainingDistance, out Vector3 point)
    62.     {
    63.         Vector3 direction = (next - start).normalized;
    64.         float contextDistance = Vector3.Distance(start, next);
    65.         distance += contextDistance;
    66.         if (distance > pointDistance)
    67.         {
    68.             point = start + (direction * (pointDistance - remainingDistance));
    69.             remainingDistance = distance = 0;
    70.             return true;
    71.         }
    72.         point = default;
    73.         remainingDistance = distance;
    74.         return false;
    75.     }