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);
}
}