Trajectory (line renderer) not showing for some angles

Hi! i’m an Unity Beginner and i’m creating a small shooter 2D for mobile.
The game is simple: there is a character located at the bottom of the screen, holding the touchscreen shows a trajectory that points towards the point touched by the player (the trajectory can bouce on the wall only once). When the player releases his finger, the character shoots.

I want to create a trajectory system exactly the same as this:
Trajectory goal

The problem is my trajectory doesn’t behave as it should, for some angles the line renderer doesn’t show up:
Trajectory angle fail

Also I can’t understand how to make sure that the trajectory is always of the same length and therefore does’t depend on the touch position (as it happens in my project):
Trajectory distance fail

This is my trajectory script:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Trajectory : MonoBehaviour
{
    [SerializeField] private float _maxDistance = 10f;

    private int _count;
    private LineRenderer _line;    


    private void Start()
    {
        _line = GetComponent<LineRenderer>();        
    }

    // Update is called once per frame
    private void Update()
    {
        _count = 0;
        _line.positionCount = 1;
        _line.SetPosition(0, transform.position);      
        RayCast(transform.position, (Vector2)Camera.main.ScreenToWorldPoint(Input.GetTouch(0).position) - (new Vector2(transform.position.x, transform.position.y))); 
    }


    private bool RayCast(Vector2 position, Vector2 direction)
    {
        RaycastHit2D hit = Physics2D.Raycast(position, direction, _maxDistance, 1<<8); 
        if (hit && _count < 1) 
        {                     
            _count++;  
            var reflectAngle = Vector2.Reflect(direction, hit.normal);   
            _line.positionCount = _count + 1;
            _line.SetPosition(_count, hit.point);
            RayCast(hit.point + reflectAngle, reflectAngle);            
            return true;
        }

        if (hit == false)
        {            
            _line.positionCount = _count + 2;
            _line.SetPosition(_count + 1, position + direction * _maxDistance);
        }
        return false;
    }

}

I really hope you can help me because I don’t know what to try

For the distance scaling problem make sure you normalize your direction so that it is always a constant length (so when you multiply with distance on line 44 you always get a vector of the same magnitude), something like _line.SetPosition(_count + 1, position + direction.normalized * _maxDistance);. For the collision problem I would guess that the problem is related… my guess is that a max distance of 10 is too small, I would try increasing it and see if you get better results. Because you are currently scaling the lineRenderer length based off of the touch distance, you are probably not raycasting all the way to the end of your line segment.