Need help with a 2D trajectory aiming system, the line renderer is giving me errors.

so here is the code am using, so far its working as i want but its giving me errors. am an artist, so i have no clue how to implement the script properly. I worked it in the update function, and it seems to work but i get weird errors. (which is understandable since am probably writing many line render arcs in the many frames during an update). . .how do i implement this code to work dynamically ?

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

[RequireComponent(typeof(LineRenderer))]
public class LaunchArcRenderer : MonoBehaviour
{
    private LineRenderer lr;
    public float vel;
    public float angle;
    public int resolution = 10;

    private float g;//force of gravity on the y axis
    private float radianAngle;
        
    void Awake ()
    {
         lr = GetComponent<LineRenderer> ();
        g = Mathf.Abs (Physics2D.gravity.y);
    }

    void OnValidate()
    {
        //check lr is not null and the game is playing
        if (lr != null && Application.isPlaying)
        {
            RenderArc ();
        }
    }

    void Start()
    {
        RenderArc ();
    }
    
     //Populates the lineRenderer with settings
    void RenderArc()
    {
        lr.positionCount =  resolution + 1;
        lr.SetPositions (CalculateArcArray ());
    }

    //Creates an array of vector3 positions for arc
    Vector3[] CalculateArcArray()
    {
        Vector3[] arcArray = new Vector3[resolution + 1];

        radianAngle = Mathf.Deg2Rad * angle;
        float maxDistance = (vel * vel * Mathf.Sin (2 * radianAngle)) / g;

        for (int i = 0; i <= resolution ; i++)
        {
            float t = (float)i / (float)resolution;
            arcArray [i] = CalculateArcPoint (t, maxDistance);
        }
        return arcArray;
    }

    //Calculate height and distance of each vertex in an arc
    Vector3 CalculateArcPoint( float t, float maxDistance)
    {
        float x = t * maxDistance;
        float y = x * Mathf.Tan (radianAngle) - ((g * x * x) / (2 * vel * vel * Mathf.Cos (radianAngle) * Mathf.Cos (radianAngle)));
        return new Vector3 (x, y);
    }

    
}

how do i pass in the velocity and angle during play time so that the arc will change in real time. also i should note that when the velocity or angle is zero i get these errors:

Invalid AABB a
Invalid AABB a
Assertion failed on expression: ‘curveT >= GetRange().first && curveT <= GetRange().second’
Assertion failed on expression: ‘curveT >= m_Curve[lhs].time && curveT <= m_Curve[rhs].time’
Assertion failed on expression: ‘IsFinite(outDistanceForSort)’
Assertion failed on expression: ‘IsFinite(outDistanceAlongView)’
Invalid AABB a

:slight_smile: i got it working. turns out the problem was coming from the velocity being zero which messed up the line renderer causing errors some how