unity-game-engine2draycasting

I am trying to use RaycastAll to check if there is an object behind the player before attacking them but it doesn't collide with anything


I have a enemy that sends a ray through the player and checks to see if there is anything behind it. The shown ray is just showing part of the line, but as you see it does collide with the player. The player has a collider attached, and I have tried setting it to normal and to as a trigger.

The ray

What I did for my code was check the length of the returned array and for some reason it returns 0. It used to work before when I used .lookAt() but that resulted in the rotation not working anymore. I am not sure what to do, I thought I understood how the raycasts worked but is there something I don't know? Thank you for the help

 bool CheckForColliders()
    {
        Vector2 direction = transform.position - player.transform.position;
        Debug.Log(Physics2D.RaycastAll(transform.position, direction, chargingRange * 2).Length); //returns 0
        if (Physics2D.RaycastAll(transform.position, direction, chargingRange * 2).Length == 1)
        {
            return true;
        }
        return false;
    }

Solution

  • V = A - B
    

    The direction of this vector is towards position A with B as the origin, so obviously you made it backwards.