So currently I am working on a pathfinding project on a 2D grid. I am casting a ray from a game object(enemy) to certain points on the grid. The following image is the situation where I think the raycasting is incorrect or at least not working the way I want it to.
I shoot a ray from the center of every unit grid (for example, the small game object that is near us is standing on (1.5, 9.5) ) toward the enemy that we see in the center of the image. If there is a hit, and the tag that it has hit has the “Enemy” tag, I have rendered a small cube in that cell to show that, that grid is visible from the enemy.
This is a snippet of my code. I fire a raycast from the center of each cell ( X.5, Y.5) toward the enemy.
I have checked that the y element for both neighborNode.m_worldPos and direction are 0 (which is intended).
I tried placed my camera’s y position on 0 manually trying to see whether or not the enemy is visible from each grid, but none of them were actually visible (from my naked eye). I made the capsule collider thin so it just barely covers his body and his lef (not his arms or head). So I dont think it would be a collider issue where I would have made the radius of the collider super big that it is visible from the 3 red cells above.
Am I missing something? I’ve been spending hours trying to figure out what might be causing it. Any help would be appreciated!
you should create lineRenderers and have tehm draw lines from where you are casting to what you’re casting at. this is an easy way to see what the rays are actually doing. you can even use the hit data so you start at the same pos as the ray and end at the same position at the hit.point.
I don’t fully understand though, you’re cubes are just a little above the surface and you’re shooting rays from their centers? does your floor not have a collider on it?
The cubes are purely for debug purpose. The screenshot of the image is a 20x20 grid, and the seeker is standing on the center of the (1,9) grid (so the position of the center is 1.5, 9.5, moving). So I am shooting a ray from (1.5, 0, 9.5) toward the enemy. I am generating the floor by using the GameObject.CreatePrimitive, and I wasnt sure how to break up each part of the grid so I can create a collider for it (unless I go through every grid and create a collider for it, but I thought that wasnt elegant). So since the enemy gameobject has a capsule collider, I am shooting a ray from the grid to the enemy.
The star was the point that I was like “there is no way that, that cell is visible from the enemy”
EDIT: I decided to draw some Debug.Lines to see the rays being casted.
I was rendering the cubes in the wrong cell!!
Thanks malkere for the suggestion of trying to draw out some lines.
right on. I’ve had headaches like that before until I start adding debug 1, debug 2, debug 3 on every freakin line to find out who is breaking the machine. drawing a line or moving a single cube around to the front of the line so you have something to actually look at can save a lot of pain in some cases.
as for spawning colliders… primitives come with colliders as you can see if you create a new one in the editor. you can also create arrays of objects via script pretty easy for going
for (int x = 0; x < 10; x++) {
for (int z = 0; z < 10; z++) {
//spawn cube at x,z or x*cubesize, z*cubesize
//remove collider if you don't want, or change color, or whatever
}
}
you can add int y in there and get 3d easy. or start adding if’s to only spawn if y > 0 when x == 5 || x == 6. easy to build walls and borders that way.