[SOLVED] Laser Sight bug plz help

Hi

I’m trying to make a laser sight for a gun object that adjusts its length based on hit colliders. This is my code.

        private LineRenderer lineRenderer;

        private RaycastHit hit;

        void OnEnable()
        {
            SetInitialReferences();
        }

        void Update ()
        {
            FireLaser();
        }

        void SetInitialReferences()
        {
            lineRenderer = GetComponent<LineRenderer>();
        }

        void FireLaser()
        {
            Vector3 fwd = transform.TransformDirection(Vector3.forward);

            if (Physics.Raycast(transform.position,fwd, out hit))
            {
                if (hit.collider)
                {
                    lineRenderer.SetPosition(1, new Vector3(0, 0, hit.distance));
                }

                else
                {
                    lineRenderer.SetPosition(1,new Vector3(0, 0, 5000));
                }
            }
        }

I’m not sure what is causing this but the line renderer origin is in the middle of the line. Also when in game the line renderer length is about 9 units long.

I’ve been working on this for a few days and no luck so far.Any help would be greatly appreciated.

Thanks

LineRenderers have an option to UseWorldSpace or not. Have you checked this is as you expect?

As Kurt said you want to make sure the line render is not using world space
as well your else needed to be moved outside of the Raycast if statment

void SetInitialReferences()
    {
        lineRenderer = GetComponent<LineRenderer>();
        lineRenderer.useWorldSpace = false;
    }

    void FireLaser()
    {

        Vector3 fwd = transform.TransformDirection(Vector3.forward);

        if (Physics.Raycast(transform.position,fwd, out hit))
        {
            if (hit.collider)//Technically dont need this
            {
                lineRenderer.SetPosition(1, new Vector3(0, 0, hit.distance));
            }
        }
        else
            lineRenderer.SetPosition(1,new Vector3(0, 0, 5000));
    }

World space is not checked. I tried this

void SetInitialReferences()
        {
            lineRenderer = GetComponent<LineRenderer>();
            lineRenderer.useWorldSpace = false;
        }

        void FireLaser()
        {
            Vector3 fwd = transform.TransformDirection(Vector3.forward);

            if (Physics.Raycast(transform.position,fwd, out hit))
            {            
                if (hit.collider)
                {
                    lineRenderer.SetPosition(1, new Vector3(0, 0, hit.distance));
                }
            }
            else
            {
                lineRenderer.SetPosition(1, new Vector3(0, 0, 5000));
            }
        }

but I’m still getting the same problem(Laser is too short). I found a temporary fix by multiplying hit.distance by 50.

Can you post a picture of whats going on? Because for me everything is working fine.

Here are the line renderer settings

A picture showing me your problem. I can’t tell if the laser is hitting the wall from that angle. Like for me I can see that the laser hits the pillar.

Here’s one of the line renderer origin problem I mentioned in the OP

Try this and see if where the actual hit is happening and if its where you think it should be

void FireLaser()
    {

        Vector3 fwd = transform.TransformDirection(Vector3.forward);

        if (Physics.Raycast(transform.position,fwd, out hit))
        {
            Debug.Log (hit.point);

            lineRenderer.SetPosition(1, new Vector3(0, 0, hit.distance));
        }
        else
        {
            lineRenderer.SetPosition(1,new Vector3(0, 0, 5000));
        }
    }

Your right that’s not where it should be. Its the same length as the line renderer but it isn’t colliding with the wall. Its like it’s hitting something infront of the gun or the z value of the raycast is being reduced by 50%

Try Debug.Log (hit.collider.name); to see what it is hitting

It’s hitting the walls

I’m building a bare test scene so i can eliminate some variables

1 Like

I’m not sure why it’s not working. This is the code that I’m using and its working fine for me.

    private LineRenderer lineRenderer;

    private RaycastHit hit;

    void OnEnable()
    {
        SetInitialReferences();
    }

    void SetInitialReferences()
    {
        lineRenderer = GetComponent<LineRenderer>();
        lineRenderer.useWorldSpace = false;
    }

    void Update ()
    {
        FireLaser();
    }

    void FireLaser()
    {

        Vector3 fwd = transform.TransformDirection(Vector3.forward);

        if (Physics.Raycast(transform.position,fwd, out hit))
        {
            Debug.Log (hit.collider.name);

            lineRenderer.SetPosition(1, new Vector3(0, 0, hit.distance));
        }
        else
        {
            lineRenderer.SetPosition(1,new Vector3(0, 0, 5000));
        }
    }

   //For debug
    void OnDrawGizmos()
    {
        Gizmos.DrawWireSphere (hit.point, .2f);
    }

Mine is practically the same(See below). I think this might not be a code issue, probably some other object or setting is conflicting with the laser script.

Thank you for all your help I did learn a few things about troubleshooting. I’ll keep working at it.

        private LineRenderer lineRenderer;

        private RaycastHit hit;

        void OnEnable()
        {
            SetInitialReferences();
        }

        void Update ()
        {
            FireLaser();
        }

        void SetInitialReferences()
        {
            lineRenderer = GetComponent<LineRenderer>();
            lineRenderer.useWorldSpace = false;
        }

        void FireLaser()
        {
            Vector3 fwd = transform.TransformDirection(Vector3.forward);

            if (Physics.Raycast(transform.position,fwd, out hit))
            {
                Debug.Log(hit.collider.name);

                lineRenderer.SetPosition(1, new Vector3(0, 0, hit.distance));
            }
            else
            {
                lineRenderer.SetPosition(1, new Vector3(0, 0, 5000));
            }
        }

Yea no problem man. Happy to help even if I just helped you learn some things about troubleshooting/debugging.

So just made a basic cube with a camera, line renderer and the laser script. It works perfectly. I’ve narrowed it down to my player object

I fixed it. I was using an empty object for the laser and when I added the line renderer it cause the origin to shift to the center of the line. I fixed this by using a cube then turning off the box collider and mesh renderer.

Once again thanks for all the help, carl and Kurt couldn’t have done this without you guys.

1 Like