How to make an object follow a spline path UE5
You have a static mesh and you want it to move to a specific location, following a specific path. Well the question is how you would do that? Let’s find out!
To get started in the content drawer right click > blueprint class > actor.
- Let’s name it SplinePath_BP
- Open the SpliePath_BP
- Add component>Spline
Create the object following this path. We’ll create an actor and use a cube as an example.
- Create Cube_BP
- Add component > Cube
- Move the cube so the base of the object is at 0. Originally then object is centered at 0.
- Head over into the event graph
Get the actor of class and assign the SplinePath_BP and create a spline reference.
We’l create a custom event called Move Object.
- Create a float variable called Time to complete (In Seconds). For this example we will set it to 5 seconds.
- We will divide 1 by our time to complete and then set the play rate.
- Create a timeline that will loop if you want it to continuosly play the timeline. The timeline will be set from 0–1. The timeline is the value of where we are going on the spline.
- We want to use that value to find where that spline is. So we’ll get the spline REF draw and get spline
- off of that we will get the spline length.
- We’ll do a linear interpolation. Where A is 0 or the start point, the Spline Length is the B value, the Timeline is the Alpha. It returns the value for our current location.
- We set the actor transform off of the timeline and split the new transform pin.
- Create another spine ref and get spline and get the location at distance along spline. Link the Lerp to the distance and change the coordinate space to World Coordinates. Connect the transform location. This will set the proper location of where the distance is on the spline
- Off of the get spline Get the Rotation at Distance Along Spline. Link the distance from the Lerp here as well
- Go back to the event begin play and add the Move Object function that was created.
- Compile and save
Time to add the SplinePath_BP to the scene.
To add to the spline click at the end of the spline
- When you have the end selected click Left Alt+mouse and move the transform gizmo to your desired spline path.
The cube is now following the path layed out for it!
So what we did is configured a path for an object to follow. You can also set the spline to be a path for a road as well by defining a static mesh for it.
Stay Tuned to the next tutorial!