Make Object follow a path and select direction when at a crossroad

Hi! I have a fox that goes through a forest. The forest has multiple paths and many crossroads, almost like a maze. The player can only control the speed of the fox by typing given words. The problem here is that my fox has to move automatically and smoothly, without player input, along said paths, and when it arrives at a crossroad the player can input the direction they want the fox to go in, thus choose the path the fox should go on. How do I do that? I’ve tried using splines, but it is too complicated to create crossroad using splines. The path has a different material on the landscape, if that helps. Thanks in advance!

I’m probably not the best person to answer this, but I have done a lot with path finding (Resource management and tower defense games). What are you using to define the existing paths to get it to go smooth? In my first game, I was using AStar. In this one, I’m using NavPath. With a NavMesh, you can alter the attributes to have smoother and rounder corners. Then you just use AIMove (assuming you’re using the AI Controller) and UE will just do it.

If you’re using C++ with custom movement, you could take a look at FInterpCurveVector. You give it the end of current path, center of intersection and the start of new path. From that you can retrieve the points you want with the resolution you want (by using Eval()) to create a path. Then append that to your existing path.

Are you using a NavMesh or custom movement? What are you using for the existing paths?

1 Like

Hi! Thanks for your reply. I’ll be honest, I’m pretty new to Unreal Engine. I haven’t heard of AStar or NavPath before, but I’ve looked them up and I know what they are now.

I have found a solution to my problem by using splines. I have 2 kinds of splines defined in different blueprints and classes (I am using both C++ and Blueprints). Let’s say there are 3 paths that intersect. These paths I’ve named “Forward Spline”. The splines that connect these 3 paths, the curves, are named “Turn Spline”. So there would be 3 TurnSplines, connecting ForwardSpline1 to ForwardSpline2, ForwardSpline1 to ForwardSpline3, and ForwardSpline2 to ForwardSpline3. If that makes sense. When my fox reaches the end of a ForwardSpline, it goes on a TurnSpline corresponding to the direction the player has chosen and at the end of that TurnSpline the fox goes on the destination ForwardSpline and so on. The problem is that setting up the TurnSplines is (while doable) very time consuming, especially in such a way that the fox goes smoothly from an ForwardSpline to a TurnSpline.

Your idea with FInterpCurveVector sounds intresting and it might be helpful. However, I don’t quite understand how to use it. There isn’t much documentation on InterpCurve on Google and I don’t exacly understand what the Eval() function in InterpCurve.h does or how to use it.

I explained how it works. There’s not much else I can explain. Just give FInterpCurveVector 3 points so you can join your splines together.

Point 1 is the end of the current path (Last point of ForwardSpline 1)
Point 2 is the center of the intersection
Point 3 is the first point of ForwardSpline2

If you don’t know point2, you can take the last segment of ForwardSpline1 and the first segment of ForwardSpline2 and create two lines and find their intersection. It’s just a line-line intersection. Plenty of code that can calculate this for you.

Now, with those three point given to FInterpCurveVector, you have a spline. You can grab points from it at any resolution by giving it a percentage along the spline from 0 to 1 with Eval(percentage) to create the spline instances that you’re using for your navigation.

So you could write a function where you give it the current spline and the next spline and it will create a new spline that connects the two.

If you want it to be even smoother, you can give it the last two points of the current spline, the intersection and the first two points of the next spline.

You can do all this either as an editor tool so that you can join all your splines as you create your level, or you can do it dynamically at runtime.

1 Like

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.