LineRenderer is shooting in wrong direction.
I have an object shooting a laser, Im using line renderer to display this for now. I have the line originating from the correct spot but for some reason its shooting backwards towards the camera (which is behind object). It should be shooting forward from the origin. Heres what I have:
public Transform gunEnd;
private LineRenderer laserLine;
void Start ()
{
laserLine = gameObject.GetComponent <LineRenderer>();
}
void Update ()
{
if (Input.GetButtonDown ("Fire1"))
laserLine.enabled = true;
laserLine.SetPosition (0, gunEnd.position);
if (Input.GetButtonUp ("Fire1"))
laserLine.enabled = false;
}
}
Changing the weaponrange from 50f to 300f allowed the linerenderer to shoot forward in the correct direction.
Answer by jigwiz · Mar 23, 2017 at 02:24 AM
After some research and adjusting code I got LineRenderer to shoot forward by adding this line:
laserLine.SetPosition (1, (gunEnd.transform.forward * weaponRange));
So the laser code is as follows:
public float weaponRange = 1000f;
public Transform gunEnd;
private LineRenderer laserLine;
void Start ()
{
laserLine = gameObject.GetComponent <LineRenderer>();
}
void Update ()
{
if (Input.GetButtonDown ("Fire1"))
laserLine.enabled = true;
laserLine.SetPosition (0, gunEnd.position);
laserLine.SetPosition (1, (gunEnd.transform.forward * weaponRange));
if (Input.GetButtonUp ("Fire1"))
laserLine.enabled = false;
}
}
Though this shoots in correct direction it shoots at a single spot in space. I want the shot to end relative to the position of the object thats shooting it (which moves around)
I changed the position of some items in the scene and now the Linerenderer is shooting in the opposite direction than its supposed to again. I didn't make any changes to the code but for some reason its shooting backward at a single point in space.
Heres the code I'm using:
{
if (Input.GetButtonDown ("Fire1"))
laserLine.enabled = true;
laserLine.SetPosition (0, gunEnd.position);
laserLine.SetPosition (1, (gunEnd.transform.forward * weaponRange));
if (Input.GetButtonUp ("Fire1"))
laserLine.enabled = false;
}
This line controls the direction the line is supposed to shot in or end at
laserLine.SetPosition (1, (gunEnd.transform.forward * weaponRange));
I tried using this too:
Laser.SetPosition(1, Cero.position + Vector3.forward * distance);
but that doesn't work too and the line is shorter
What am I missing here? I also tried rotating the gunEnd game object but still no good.