LineRenderer. Problem with ray displaying

Hello. What am I doing wrong? Here is the code and screenshots with my problem(
When the beam hits the object, it is reduced.
P.S. Im not use WorldSpace coordinate

public class Emitter : MonoBehaviour {

    private LineRenderer lr;


	void Start ()
    {
        lr = GetComponent<LineRenderer>();
	}
	
	void Update ()
    {
        RaycastHit hit;
        if (Physics.Raycast(transform.position, transform.forward, out hit))
        {
            if (hit.collider)
            {
                lr.SetPosition(1, new Vector3(0, 0, hit.distance));
                print("Hit: " + hit.distance);
            }
        }
        else
        {
            lr.SetPosition(1, new Vector3(0, 0, 100));
        }
    }

}

81204-2.jpg

Since you are not using WorldSpace in Line Renderer, you’ll need to convert the end position/distance to the Local Space. Something like this:

float dist = transform.InverseTransformVector(transform.position - hit.point).magnitude;
lr.SetPosition(1, new Vector3(0, 0, dist));

Update: If i use debug.drawray - hit.distance work perfectly

      if (Physics.Raycast(transform.position, transform.forward, out hit))
            {
                Vector3 forward = transform.TransformDirection(Vector3.forward) * hit.distance;
                Debug.DrawRay(transform.position, forward, Color.green);
...

81213-3.jpg