How do I set a max Length for my LineRenderer?

Hello People.
I’m really new to Unity and Programming. I’ve been using Construct 2 before and things like that.

I have currently a working Line starting from my Object and ending at my Mouse. Means i can drag the line infinitely long which i don’t want to.
How can i set a max length of the Line?
(In my current script, I changed the color from the line to red if it reaches the length of 6.7.)

public class TrajectoryLine : MonoBehaviour
{
    public LineRenderer lr;

    public Color defaultStart = new Color(1.0F, 1.0F, 1.0F, 1.0F);
    public Color defaultEnd = new Color(1.0F, 1.0F, 1.0F, 0.0F);
    public Color maxStart = new Color(1.0F, 0.0F, 0.0F, 1.0F);
    public Color maxEnd = new Color(1.0F, 0.0F, 0.0F, 0.0F);

    private void Awake()
    {
        lr = GetComponent<LineRenderer>();
    }

    public void RenderLine(Vector3 startPoint, Vector3 endPoint)
    {
        lr.positionCount = 2;
        Vector3[] points = new Vector3[2];
        points[0] = startPoint;
        points[1] = endPoint;
        lr.SetPositions(points);

        if(Vector2.Distance(startPoint, endPoint) > 6.7)
        {
            lr.startColor = maxStart;
            lr.endColor = maxEnd;
        }

        else
        {
            lr.startColor = defaultStart;
            lr.endColor = defaultEnd;
        }
    }

    public void EndLine()
    {
        lr.positionCount = 0;
    }
}

I would use this function to limit the distance between the two points Unity - Scripting API: Vector3.MoveTowards

For example:

        Vector3[] points = new Vector3[2];
        points[0] = startPoint;
        points[1] = Vector3.MoveTowards(startPoint, endPoint, 6.7f);
2 Likes

It works perfectly, thank you so much!