How to make LineRenderer in GameObject Facing?

I’m trying to Draw a line in front of my character but its not working.
It’s my code

I Tried this

   RaycastHit2D hitInfo = Physics2D.Raycast(transform.position, transform.forward, 50);
    if (hitInfo.collider != null) {
        GetComponent<LineRenderer>().SetPosition(0, transform.position);
        GetComponent<LineRenderer>().SetPosition(1, hitInfo.point);
    } else {
        GetComponent<LineRenderer>().SetPosition(0, transform.position);
        GetComponent<LineRenderer>().SetPosition(1, transform.position + transform.forward * 50);
    }

and i tried this

    RaycastHit2D hitInfo = Physics2D.Raycast(transform.position, transform.forward, 50);
    if (hitInfo.collider != null) {
        GetComponent<LineRenderer>().SetPosition(0, transform.position);
        GetComponent<LineRenderer>().SetPosition(1, transform.forward * 50);
    } else {
        GetComponent<LineRenderer>().SetPosition(0, transform.position);
        GetComponent<LineRenderer>().SetPosition(1, transform.forward * 50);
    }

My LineRenderer not getting Y axis, how i correctly this? I’m using Joystick to rotate the player.

You’re using Transform.forward which equals to (0, 0, 1). If you’re moving and rotating on the X and Y axes, perhaps try using Transform.up?

edit:

            GetComponent<LineRenderer>().SetPosition(0, transform.position);
            GetComponent<LineRenderer>().SetPosition(1, transform.up * 50);