Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

 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.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading