- Home /
Having trouble using a line renderer to show a projectile's predicted trajectory.
I have a script that I have been using to show a projectile's predicted trajectory in a 2D game. It is basically a click, drag and release slingshot. This script currently creates an array of primitive sphere gameobjects to show where the projectile is going to go. It works great and lines up with the projectile's path perfectly, but it doesn't look all that great and I really don't want to have it creating all of those spheres every time the player takes aim. I have been trying to use a line renderer to take the place of the spheres, but after looking up every line rendering and trajectory tutorial I could find, I still can't figure out what I'm doing wrong.
I have looked over the line renderer scripting API many times and it's clear I'm supposed to be using the SetPosition function, but with my own formula. I just can't figure out how to get it to work for me. I think the problem has to do with the number of vector3 positions in the line renderer getting set wrong at some point. If I check the positions before touching anything, it shows the amount set in the Inspector, however as soon as I click on the projectile, it gets set to 0, which I'm guessing is why the line renderer is not shown. I'm just not sure what is causing it to get reset.
Here is my current code (with the sphere method). If someone could tell me the correct way to apply a line renderer to this I would be extremely grateful.
var force = 11; // Distance the dots cover. Must change projectile's
//launchForce to equal amount to follow path correctly.
var samples = 40; // Number of dots. Increase to extend how far the line reaches.
var spacing = 0.1; // Time between samples
private var offset : Vector3;
private var home : Vector3;
private var argo : GameObject[];
private var velocity = Vector3.zero;
var projectileLoaded : boolean;
function Start ()
{
argo = new GameObject[samples];
for (var i = 0; i < argo.Length; i++)
{
var go = GameObject.CreatePrimitive(PrimitiveType.Sphere);
go.collider.enabled = false;
go.transform.localScale = Vector3(0.4, 0.4, 0.4);
argo[i] = go;
}
}
function ShowHideIndicators(show : boolean)
{
for(var i = 0; i < argo.Length; i++)
{
argo[i].renderer.enabled = show;
argo[i].transform.position = home;
}
}
function OnMouseDown()
{
if(projectileLoaded)
{
home = transform.position;
ShowHideIndicators(true);
}
}
function OnMouseDrag()
{
if(projectileLoaded)
{
DisplayIndicators();
}
}
function DisplayIndicators()
{
argo[0].transform.position = transform.position;
var v3 = transform.position;
var y = (force * (home - transform.position)).y;
var t = 0.0;
v3.y = 0.0;
for (var i = 1; i < argo.Length; i++)
{
v3 += force * (home - transform.position) * spacing;
t += spacing;
v3.y = y * t + 0.5 * Physics.gravity.y * t * t + transform.position.y;
argo[i].transform.position = v3;
}
}
//////////////////////////////////////////////////////////////////
/////// I know I need to use the SetPosition function shown here,
/////// but don't know how to fit it with my current formula.
var pathNodes : Vector3[];
function Start()
{
var lineRenderer : LineRenderer = gameObject.AddComponent.<LineRenderer>();
}
function Update()
{
var lineRenderer : LineRenderer = GetComponent.<LineRenderer>();
lineRenderer.SetVertexCount(pathNodes.length);
for (var i: int = 0; i < pathNodes.length; i++)
{
lineRenderer.SetPosition(i, pathNodes[i].transform.position);
}
}
@SBull I am looking into doing the same thing, were you able to find a way to do this?
Unfortunately, no. I settled for swapping the spheres out with alpha textured planes.
You obviously need "pathNodes" to be initialized properly for your Update function to work, but I don't see where you set it up. It looks like DisplayIndicators()
AL$$anonymous$$OST does this, but it assigns the computed vector to argo[i].transform
(your spheres I think), rather than pathNodes[i]
. Don't forget, you'll probably need a pathNodes = new Vector3[samples]
line somewhere too!
also, this lines looks odd:
lineRenderer.SetPosition(i, pathNodes[i].transform.position);
if pathnodes is an array of Vector3's; pathNodes[i] doesn't have a transform.position, rather it IS a position.
lineRenderer.SetPosition(i, pathNodes[i]); //Is what I would have expected