x


Can't get a laser working properly.

I'm using a LineRenderer to trace a laser. The second point of the LineRenderer has to be wherever it's coming in contact with a collider, and if it's not contacting one, it should maintain a set length.

This isn't working at all. It's just INCREDIBLY unstable and makes these weird, random movements (the end point sways around at random).

Not sure what I'm doing wrong here, as the code is as basic as it gets.

It pretty much works the way I need it to, except that as my character moves left and right, the end point of the laser sways GREATLY left and right, resulting in a laser that won't stay straight unless I'm in the middle of the screen.

void SetLength()
    {
        RaycastHit LaserHit = new RaycastHit();
        if (Physics.Raycast(new Ray(gameObject.transform.position, -Vector3.forward), out LaserHit))
        {
            transform.rotation = Quaternion.Euler(new Vector3(0, 0, 0));
            if (LaserHit.collider.tag == "Enemy")
            {
                SetLaserEndpoint(gameObject.transform.position + (new Vector3(0, 0, -LaserHit.distance)));
            }
        }
        else
        {
            SetLaserEndpoint(gameObject.transform.position + (new Vector3(0,0,-10)));
        }
    }

    void SetLaserEndpoint(Vector3 point)
    {
        lineRenderer.SetPosition(1, point);
    }
more ▼

asked Aug 11 '11 at 09:51 PM

Jason B gravatar image

Jason B
1.7k 29 32 44

And as you can see, I even tried setting the rotation to 0,0,0 every time SetLength is called (about 10 times per second) so that I could rule out that it's somehow rotating. I also neglected setting the endpoint's X and Y values and locked them to 0 to ensure the endpoint doesn't misalign from the start point. Apparently does no good.

Aug 11 '11 at 09:52 PM Jason B
(comments are locked)
10|3000 characters needed characters left

2 answers: sort voted first

I can only guess, but is your gameObject the parent in this situation? you may want to make sure the transform.position you are using isn't local space.

If that isn't the problem, I would go through and print out just about every number in there to see where it goes wrong.

print(position/rotation/collider/laserEndpoint/etc.)

more ▼

answered Aug 11 '11 at 11:04 PM

DoubleDouble gravatar image

DoubleDouble
42 6 6 13

(comments are locked)
10|3000 characters needed characters left

Set your LineRenderer to useWorldSpace. It is also odd that you are setting transform.position when there is a hit - why it that going to matter, since your rays using global forward, not forward of the transform? (and I think you want Quaternion.identity, not the same thing calculated from Eulers).

more ▼

answered Aug 11 '11 at 11:28 PM

Waz gravatar image

Waz
6.4k 22 33 70

(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x5070
x4150
x1687
x1528
x55

asked: Aug 11 '11 at 09:51 PM

Seen: 932 times

Last Updated: Aug 11 '11 at 11:29 PM