I have an AI character that moves around a grid. Said enemy character can only move onto certain types of tiles that make up the grid - however, these tiles are generated dynamically by the player as he moves around the game grid.
I need a way for the AI to ‘see’ the tiles around it, and then be able to determine if and where any appropriate tiles are positioned relative to itself, and then be able to randomly select one of the tiles to move onto.
I tried to do this using multiple Physics.raycast calls and a switch statement tied to a randomly generated number. However, the behavior is completely unreliable, and seems to indicate from my observations that rather than four rays being generated (one in each direction it is possible to move on the grid), only one is.
For some reason I started getting the feeling that I had read somewhere before that a gameobject can only generate one ray at a time.
Is there some other way to do this? I looked at the waypoints in the racing tutorial, but honestly can’t think of a way to dynamically assign the ‘next’ waypoint segments of each waypoint as a tile is created by the obviously unpredictable human player.
Thats certainly not the case. Raycast calls are executed immediately and you can call it as often as you want.
It seems that using Raycasting for finding the neighboring cells is overkill though. If you generate the cells from code anyway, then maybe you should keep an array which represents a 2D table of all cells. Then you can visit the neighboring cells easily by accessing the neighbors in the 2D table.
Hrrrm. Well I’ll probably go the way of the table - but it still frustrates me why my raycast code isn’t working. Heck, even the Debug.DrawRay doesn’t seem to be doing anything. Is there something I’m missing here? (Code below)
never mind. Got it to work. Got lost in the morass of code and just needed to fix a few small things (the tiles are below the transform.position of the AI character, and aren’t tall enough for a ray originating there to ‘see’ their collider)