Spline component Unreal Engine 4
As a beginner in Unreal Engine 4, many people wonder how they can make their character to follow a curved path. Then they come to know about Spline components using which they can set up a curved path easily into their level. Now they need to make their character in such a way so that it can follow this spline path or custom curved path . So in this post we will be looking into some of the basic functions of Spline component which we can use to make our character move on spline automatically.
- First of all, create a new project in Unreal Engine 4 and use Third Person Template Blueprints for your project. You can choose some other template too, as per your wish.
- Now go to “Blueprints” folder in your ThirdPersonGame and create a new Blueprint Class of type Actor. We are naming it as “SplineBP” for this tutorial.
- Now open this Blueprint.You will find “Add Component” option to top left, after opening this blueprint. Search for a Scene component and add it. Drag this scene component over your Default Scene Component to get rid of default Sphere in viewport of your blueprint. Now click on Add Component again to find “Spline” and add it to your blueprint. Compile and save your blueprint.
- Open your level blueprint and drag this newly created Spline BP into your level. Now select the spline point and use “ALT + drag” to add more spline points to your spline. Now you can create a predefined path in your level using this Spline BP.
- Now in your Blueprints folder, find ThirdPersonCharacter Blueprint and open it. You will find default functionalities in this blueprint related to your character movement. We will be working in “Movement input” section of event graph to make our character move along Spline.
We will create two variables here. First variable of type “Spline BP” which will store reference to our Spline BP. Second variable named “SplineDistance” of type float which will store the distance traversed along the Spline path.
- Now take the Spline BP reference in your Character Blueprint and find a node “Get World Location at Distance along Spline” and select it. Pass the SplineDistance variable that we have just created into the Distance field of the node. Similarly look for “Get World Rotation at distance along Spline”.
- Now find nodes “Set Actor Location” and “Set Actor Rotation”. Now pass the outputs of nodes in above step to these nodes. It will set the Rotation and Location of your Character. As we want this to update constantly, we will do these changes inside tick event. Find the “Input axis move Forward ” node that was present by default in Movement Input section of your Third Person Character Blueprint. Replace this node with “Event Tick” and set the Scale value in “Add Movement Input” node to 1.
- This will unable our Character to update its location and rotation continuously. But still you will find there is no movement in character, because the “Spline Distance” variable is fixed at value 0.0 and at every frame, location and rotation of starting point of Spline is being set to the character. So we need to add some value to our Spline distance every frame, which will make our character to move that particular distance every frame on our spline.
- The final Blueprint will look something like below.
- Compile and Save. Still you will find error, because the Spline BP reference is having no value yet and is null. Open your Spline BP Blueprint and go to its event graph. Here in Event Begin Play method set the value of Spline BP variable which is in Third Person Character. Check Blueprint Communication in detail here.
That’s it. Compile and Save , you are good to go now. Please let us know if you are facing any problem. We’ll be more than happy to help.