- Home /
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!
On a quick read, I suspect line 5 and 8 of the last script. You are using Vector3.forward ins$$anonymous$$d of transform.forward. And you you are calculating a position projected from the origin. I suspect the first should be:
lRenderer.SetPosition(1, (hitInfo.point - transform.position));
and the second line should be:
lRenderer.SetPosition(1, (transform.forward * maxDistance));
Yeah, I did think about that, but it doesn't seem to make any difference, presumably because the line renderer is set to use local axis anyway. After messing around with it some more today however, I found that if I change the mesh of the projectile that is spawning the laser/line renderer, it works fine. Do you happen to know why this is? It's leaving me rather confused.
Ahhh, the world coordinate thing. Without "Use World Space" checked, all the coordinates are relative to the the position of the game object. So for your example above, you need to set the original position to Vector3.zero, rather than the origin point of the laser. And I've edited the code above for the second point.
Not sure why changing the mesh made thing work unless you changed the position to the origin (i.e. where relative and absolute coordinates are the same).
While that didn't solve the problem, you did get me thinking about world space. Since the line renderer seemed to be having the issue with local space, I converted my code to world space (thanks to what you said). I don't know why it didn't like local space with certain meshes, but I guess that might be a problem with Unity itself. Anyway, thanks a lot for the help, I'll post what I did as the answer.
Answer by F1refly · Mar 05, 2013 at 03:28 PM
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));
}
}
Your answer
Follow this Question
Related Questions
Linerenderer view rotation in local space 1 Answer
Rotating a LineRenderer 0 Answers
Fixing LineRender 3d rotation 0 Answers
Line renderer rotation and alignment 1 Answer