The issue is, I can never get the raycasts to actually hit anything. There are objects in my scene with 2d colliders. This object is instantiated, and it should also be returning raycast collisions with the clones of itself (they spawn in basically the same exact spot). I think my issue has something to do with the position or direction of my raycast, but I have no idea.
Please save me from my eternal plight (Barely knowing C#) and help me fix this code.
You transform the direction of Vector2.left and Vector2.right for Debug.DrawRay but you don’t do it for the actual ray casts.
If you do intend to ray cast to the left and right of the object then you can use transform.right and -transform.right instead of transforming the left and right vectors.
This was one of the many issues with my code, Apparently Physics2D.RaycastAll doesnt actually work in 2D, for some reason. Im going to fix this using GetRayIntersection (Outlined here Raycasting in 2D with new InputSystem).
The post you linked to was stating that the 3D version of Raycast doesn’t work in 2D. The 2D version of Raycast works fine.
Perhaps the objects you’re trying to ray cast against all have 3D colliders attached?. If so, you’ll need to remove the colliders and replace them with 2D colliders.
The reason being that this is absolutely made up and completely wrong.
What has that article got to do with your code above though? That article is trying to query along the Z plane (in/out) the screen to see if a collider intersects the mouse. Clearly there is no 3D in 2D physics so you’d use “Physics2D.OverlapPoint”. I implemented the GetRayIntersection stuff for when you wanted to do a pseudo 3D ray check on 2D colliders but it’s smoke and mirrors with sorting and is rarely required.
This is basic stuff so you must be doing something completely wrong such as using 3D colliders as mentioned above.
In 3D you cannot start a query inside a collider as it’ll always return no results. In 2D it will by default but is controlled but the following option which you may have turned off: Unity - Scripting API: Physics2D.queriesStartInColliders
I think you should show more than the snippet above as this stuff just works out the box without issue.
I have scrapped the raycasts idea. Not happening lmao. I think its not working because of how I have my layers organized in my scenes. I could probably organize a new project to test it, but I’m not getting that industrious for now.
So you know, it makes no difference what layers you’re using, you’re not even specifying which layers you want in the queries you showed so it’ll return any layer therefore it’s nothing to do with layers.
Im doing a game jam and wanted to be able to show the people at the convenction (Who are all random business owners from the city, who know nothing about programming) that the AI was actually going around and “Looking” for the player, and I was going to do it with raycasts. Instead Im just going to reference the players position directily. ( I stole this idea from a tiktok that I watched)
Im telling you man, I tried like everything lmao. I spent like 3 hours cummulatively just trying to get my enenmies to raycast my player. I was able to get raycasts working when I tested them with objects that Were NOT my enemies, I even got them working from my Player Object. I could however never get my enemy characters to raycast anything at all. Only thing I can think of is with my layers. Unless its something INSANELY basic that I just skimmed over somehow, Im out of things to try.