This tutorial will teach you how to move actor along spline in Unreal Engine.
In this step-by-step how-to guide, you will learn how to move actor along spline or move object along spline in Unreal Engine.
1. Create an actor with the Spline component
First, we’ll need our actor with the spline — the actual path our character will follow. Create a new blueprint of the Actor type and call it BP_Path. Add the Spline component and save. That’s it, no need to do anything else with this actor. You’ll end up with a straightforward hierarchy:
2. Create the actor that you’re going to move along the spline
If you don’t already have the object you want to move, create one. Mine’s called BP_Courier. Add a static or a skeletal mesh component to it so that it’s visible in the game world, and don’t forget to set this component up with an actual object.
3. Create three variables
Create three new variables in your BP_Courier:
Alphaof type float — it’s going to be used in the updating of our actor’s location and rotation.
Durationof type float — with this, you’ll set up the actual length of the movement.
Path Refof type BP_Path (Object reference) — this is a reference to the path actor we’ve created in step 1. Make it public.
4. Create a timeline
Right-click in the blueprint graph and type in “timeline,” then select Add Timeline action. You can call it whatever you like; I’ve called it Move.
Double-click the timeline to open it for editing. Next, create a new float track by clicking the button with the f+ symbol. Call it Alpha.
Add two keyframes by clicking Shift + Left Click in the float track. Select the first one and set both the Time and the Value to 0,0. The second one should have Time and Value set to 1,0 and 1,0 accordingly.
Finally, set the Length of the timeline to be 1.00. Basically, you want to make your float track go from 0 to 1 in the span of 1 second linearly. You should end up with this:
5. Set up the actor’s location and rotation to be updated with the timeline
Use the SetActorLocationAndRotation node to update our actor’s location and for our alpha to be stored in a variable:
As you can see, I’ve collapsed the nodes that provide the actual rotation and location into a single node. So here’s how it looks on the inside:
6. Hook up the duration
Now let’s utilize our Duration variable. I’ve created a custom event called MoveTheDummy to finalize the whole movement into a single event. Here we first set the play rate of the timeline and then start it:
All you need to do here is to set up the default value for the Duration in seconds.
After this, you can call the MoveTheDummy event however you like — I’ve plugged it into the BeginPlay event. We’re now finished with the scripting. Let’s go set up our scene.
7. Add actors to the scene and create the Spline path
Finally, to move our object along the path, you need to drag your BP_Courier (or whatever you’ve called it) and BP_Path into the scene. Then, click the BP_Path to create the actual path your character/object is going to follow.
You can either check the official documentation on this or use a time-saving lifehack of right-clicking precisely into the spline point and selecting Spline Generation Panel from the menu. This will open a handy tool that will allow you to quickly create a spline path in the form of a certain shape.
Select your BP_Courier and fill our public Path Ref variable with the BP_Path from our scene in the Details panel. That’s it!