• News

• Industries

• Learning & Support

• Community

• Marketplace

# Right way around a maze?

Guys I’ve got a maze, I’d like a message to pop up if the player goes the wrong way, how can this be realised in ue4 in an easy way and is there an example anywhere?

Help much appreciated!
D.

Finding shortest path in maze is not trivial. If you wrote algorithm and generated that maze from code, there are not so hard solutions (compared to code for creating maze) to find shortest path during maze generation.
Only other solution that is half ready in unreal, would be EQS (enviromental query system). However it is quite advanced part of unreal. You need to understand events, behavioral trees, and then experiment some with EQS itself.

For EQS, finding shortest part should be quite basic task (if you learned EQS). Create function that returns navigation distance to some point, as its point set exit location from maze. Then create grid of query points evenly spread trough maze. Pick spot that is next to player and has smallest distance to exit. That point is where player should head.

I doubt that there is tutorial for exactly what you want.

You could write a blueprint to walk the maze and store the correct value at each point, and then compare against that perhaps? It would assume that you have a single 1d linear path that is correct. It would help to know how your maze works before suggesting any implementation details.

Oh I don’t need anything that complicated (I assume) I just need something that if the player goes backwards (turns round) instead of forwards it says “wrong way!”

The maze itself is a bsp landscape environment that isn’t procedurally generated, it uses a branching route with only one possible correct route with very short dead ends as wrong turns.

I’d really appreciate your help guys.

How long is your maze? If its not that big, I would probably just do this manually by placing editable vector points in order or something like that. Then you can always do a dot product with the players forward direction and the vector between the 2 closest points on the maze path. If the dot is negative, player is going wrong way.

In order to find the point the player is on you could store which point they are at (maybe just set it to 0 to start if there is a known entrance etc), and always calc the distance between the current and previous and next point. Then if the distance to next point is closer, you make that the new Current point and so on. If the distance to previous point becomes greater again, thats another way to determine wrong direction independent of orientation if you need it.
That is only a really basic idea of how to start and there may be unforseen issues.

The maze takes about 3 minutes to complete, it’s not massive.

Is there an example of something that I could follow that’s been used in landscape, I’m not a programmer and pretty new to ue4 too.

I am sure you could figure out out using the navigation system, but for something this specific I would just do it manually. This is just an ultra quick prototype of how it could work.

This is 2 blueprints. One is the Maze Points BP, and all it does is contain a linear list of points, and the construction script draws lines between the points for visualization:

Then there is another BP which I have called FakePlayer that has a reference to the maze. It has an Int variable for “Current Segment” and it is always checking if you are closer to the current or next segment, and checks the forward direction vs current segment. It turns red if not facing the path and advances the segment when the object is closer to the next segment in the maze. It draws a debug sphere at the endpoint of the current segment.

Here is how you can compare distance from current and next segment to see if player is advancing (or you could just use a distance to point which is easier):

And checking the direction is easy using a dot product. The result is the cosine of the angle. You can do acos(degrees) to get the degrees away from the current segment forward if you need it:

Cheers for that Ryan, it looks pretty complicated to implement, I’d like to give it a shot though, is there any reading material on the functions as I’m not a programmer by a long chalk!

Much appreciated Ryan!