Raycast miss corner


Hello I am currently using a single raycast to check for points in world positions from my player camera. the problem is the min distance between each point can sometimes make the ray misses the corner of the wall which can cause the line to go through the wall. I can make the min distance float very small but it’ll make unnecessary points that will effect performance. I thought since I have the hypotenuse of the corner tringle maybe using it to get the point in the corner but I dont think this is the best way to get it. even if its the best way I dont get how can I add the point in the middle of the list since I already made other points after it I really hope someone can help me with this.

this is my code:

public class Line : MonoBehaviour
{
    [SerializeField] private float minDis = 0.1f;
    [SerializeField] private List<Vector3> points = new List<Vector3>();

    private Camera cam;
    private Vector3 lastMousePos;

    private void Awake()
    {
        cam = Camera.main;
    }

    private void Update()
    {
        if (Input.GetMouseButtonDown(0))
        {
            AddPoint();
        }

        if(Input.GetMouseButton(0))
        {
            float dis = Vector3.Distance(GetMousePosition(), lastMousePos);

            if (dis > minDis)
            {
                AddPoint();
            }
        }

        if(Input.GetKeyDown(KeyCode.R))
        {
            points.Clear();
        }
    }

    private Vector3 GetMousePosition()
    {
        Ray ray = cam.ScreenPointToRay(Input.mousePosition);
        Physics.Raycast(ray, out RaycastHit hit);

        Vector3 vec = hit.point;
        return vec;
    }

    private void AddPoint()
    {
        if(GetMousePosition() != Vector3.zero)
        {
            points.Add(GetMousePosition());
            lastMousePos = GetMousePosition();
        }
    }
}

It looks like you are trying to create a line in 3D space using a series of points, and you want to ensure that the line accurately follows the corners of objects in the scene.

One way to solve this problem is to use multiple raycasts, instead of just a single one. You can create a raycast in the direction of the player’s camera, and then create additional raycasts in the directions that are perpendicular to the first one. These additional raycasts will help you “catch” the corners of objects that the initial raycast may have missed.

Here is an examples you can checkout:
public class Line : MonoBehaviour
{
[SerializeField] private float minDis = 0.1f;
[SerializeField] private List points = new List();

    private Camera cam;
    private Vector3 lastMousePos;

    private void Awake()
    {
        cam = Camera.main;
    }

    private void Update()
    {
        if (Input.GetMouseButtonDown(0))
        {
            AddPoint();
        }

        if(Input.GetMouseButton(0))
        {
            float dis = Vector3.Distance(GetMousePosition(), lastMousePos);

            if (dis > minDis)
            {
                AddPoint();
            }
        }

        if(Input.GetKeyDown(KeyCode.R))
        {
            points.Clear();
        }
    }

    private Vector3 GetMousePosition()
    {
        // Create the initial raycast in the direction of the player's camera.
        Ray ray = cam.ScreenPointToRay(Input.mousePosition);
        Physics.Raycast(ray, out RaycastHit hit);

        Vector3 hitPoint = hit.point;

        // Check if the raycast hit anything.
        if (hitPoint != Vector3.zero)
        {
            // Create additional raycasts in the directions that are perpendicular to the initial raycast.
            Vector3 right = Vector3.Cross(ray.direction, cam.transform.up);
            Vector3 up = Vector3.Cross(right, ray.direction);

            RaycastHit hitRight, hitUp;
            Physics.Raycast(hitPoint, right, out hitRight);
            Physics.Raycast(