Maze-game control question

G'day guys, I'm working on a small project - a 'maze game', akin in movement to Pacman. I've built a player movement system with three values - 'desiredDirection', 'availableDirection' and 'actualDirection'. You can guess how these are used easily enough - the player can set their next desired direction change, and as soon as they reach a junction where that direction is available, they change course.

I've got the player moving anyway they wish (North, South, East or West), but 'availableDirection' hasn't been implemented yet. The way I see it i have three techniques available to me, and I'm seeking advice on which is best:

1) Scripted junctions = Write script that basically creates the junction points through pure code, ie "if player position = junction position, available directions are etc."

2) Physical junctions = Collisions with invisible boxes - 'on collision, available directions are..."

3) Raycasting = Saw this in a post on answers. Feels pretty risky to me, but it could be great, as it'd be dynamic and wouldn't need adjusting per 'maze'.

If you guys could help steer me in the right direction, I'd be most appreciative. I've tried getting the collision technique to work, but for some reason i just can't get a cube to react to a collision with my player object.

And no I'm not looking to build Pacman. :P

I would go with RayCasting.

Check out the scripting reference here:

For example this:

function Update () {
var hit : RaycastHit;
if (Physics.Raycast (transform.position, transform.forward, hit, 2.0)) {
print ("There is an object in front of the player");

This will detect if there is an object 2 metres in front of the player and print the line "There is an object in front of the player" to the debug console.

You could have 4 raycasts. The one above for forward, and 3 more for your other directions (just change the transform.forward part of the raycast to cast the ray in the other directions).

Finally for debugging you can use the DrawRay Class to actually visualise the RayCast:

I haven't given you the exact script you need here, just a few nods in the right direction. It''s easier to learn this way :-)

If the game really is like Pacman, then the maze is on a grid, in which case the fastest/easiest thing is to have a 2D array that represents the maze, and you get available directions from the array. For example, Realmaze3D has no colliders, raycasting, or anything like that, just a 3D array of integers.

Sorry for closing this so late, but I've since built and completed a working and efficient system. I could have created the X/Z array, but in this instance I've created 'if' statements detecting the player location (if transform.position.x == 0 && transform.position.z == 3) etc. instead. I suppose the reason I didn't go with the array was multimensional array support seems a bit weird in Unity.