Weird Linecast 2d behaviour

I have an object, that’s going to check if there’s a similar object within a certain distance next to it. My intent was to do this with a linecast. However the linecast is behaving oddly.

In my test environment, when I start running the scene the linecast wont register any hits. If I move the parent of the casting object around it might start registering hits from the targets, sometimes without even hitting them. Also sometimes the hits are from the parented object itself, even though the casting object is outside it’s collider.

    [SerializeField]
    private Transform castPosition;

    float baseCastDistance = 1f;

    private void Update()
    {
        CastEast();
    }

    void CastEast()
    {
        float castDistance = baseCastDistance;
        Vector3 targetPosition = castPosition.position;
        targetPosition.x += castDistance;
        Debug.DrawLine(castPosition.position, targetPosition, Color.cyan, 1f);

        RaycastHit2D hit = (Physics2D.Raycast(castPosition.position, targetPosition, 1 << LayerMask.NameToLayer("Blocks")));

        if (hit)
        {
                print("Item in the way: " + hit.rigidbody.gameObject.name);
            
        }
    }

198311-asdas.png

In the 18th line you’re using Raycast instead of Linecast, and the Debug.DrawLine shows you the line, not the ray. Maybe that’s the problem, but I’m not sure.