Line Renderer Rotation Issue

I’ve been pulling my hair out over this all day and I’ve finally given up and decided to come looking for help.

So anyway, the problem at hand: I have a line renderer that I am using as a laser (GameObject with Transform, Line Renderer and Script attached); it’s essentially a copy of the one from AngryBots.

However, I am applying a rotation to its transform in order to make the laser point in different directions (along the transform’s z-axis). This works fine when instantiated from the weapon but when I try to instantiate it from another projectile, it doesn’t work.

For some reason the line renderer refuses to draw the laser correctly (even though the spawn script is the same), the transform rotates as expected and the z-axis has the right facing but the laser does not point along it.

This is how I am spawning the laser:

function SpawnProjectile() {
	for (var i = 0; i < numberOfProjectiles; i++) {
		var clone : GameObject = Instantiate(projectile, projectileSpawn.position, projectileSpawn.rotation);
		
		if (projectile.tag == "Laser" || projectile.tag == "Conical") {
			clone.transform.parent = transform;
		}
		
		if (releaseAfterTime && releaseRate > 0) {
			yield WaitForSeconds(releaseRate);
		}
	}
}

This function is in both the weapon and the projectile scripts that I am instantiating the laser from.

I am then applying the rotation in the script attached to the laser object, like so:

function Start () {
	var randomShotVariance = Quaternion.Euler(Random.Range(minXVariance, maxXVariance), Random.Range(minYVariance, maxYVariance), 0);
	transform.rotation *= randomShotVariance;
}

As mentioned, this seems to work perfectly fine when the laser object is instantiated from the weapon, but has the aforementioned problem when spawned from the projectile and I cannot figure out why.

Just for context, this is how the laser is being drawn:

function Update () {
	NormalRaycast();
		
	if (hitInfo.transform) {
		lRenderer.SetPosition(1, (hitInfo.distance * Vector3.forward));
	}
	else {
		lRenderer.SetPosition(1, (maxDistance * Vector3.forward));		
	}
}

function NormalRaycast() {	
	Physics.Raycast(transform.position, transform.forward, hitInfo, maxDistance);
}

Element 0 of the line renderer is naturally the origin point of the laser and Element 1 can be seen above.

I must stress that this works perfectly fine when on the weapon, but not when on the projectile.

Thanks in advance!

I converted it all to world space in the end, and it works with whatever mesh I am using - strange, but at least it works. Thanks to robertbu though for the assistance!

Here’s what I changed it to:

function Update () {
	lRenderer.SetPosition(0, transform.parent.position);	

	NormalRaycast();
		
	if (hitInfo.transform) {
		lRenderer.SetPosition(1, transform.parent.position + (hitInfo.distance * transform.forward));
	else {
		lRenderer.SetPosition(1, transform.parent.position + (transform.forward * maxDistance));		
	}
}