Spline-Based Camera System
admin2024-09-24T11:34:41+00:00What You’ll Learn:
- How to create a camera that follows a spline instead of being attached to the player.
- How to set up the spline blueprint and character blueprint to achieve smooth camera motion.
Step 1: Create a Camera Spline Blueprint
1.1 Create a New Blueprint
- Open your Content Browser (
Ctrl + Space
). - Right-click in the Content Browser, go to Blueprint Class, and select Actor.
- Name the blueprint BP_CameraSpline.
1.2 Add a Spline to the Blueprint
- Open the BP_CameraSpline blueprint.
- In the Components tab, click Add Component and search for Spline (not Spline Mesh).
- Click Compile and Save.
You now have a simple spline component inside the blueprint that can be placed and modified in your level.
Step 2: Modify the Character Blueprint
2.1 Event Begin Play
- Open your Character Blueprint (e.g.,
BP_ThirdPersonCharacter
or similar). - In the Event Graph, right-click and add an Event Begin Play node.
- From Event Begin Play, drag out and search for Get Actor of Class. In the dropdown, select BP_CameraSpline (the blueprint you just created).
- Right-click the return value from Get Actor of Class and select Promote to Variable. Name it CameraSplineReference.
2.2 Event Tick
Now, we will set the camera’s position to follow the spline smoothly.
- Right-click and search for Event Tick.
- From Event Tick, drag off and search for Get Follow Camera.
- Drag off from Follow Camera, and search for Set World Location.
- Connect Set World Location to Event Tick.
2.3 Get the Spline’s Location
- Drag out your CameraSplineReference and search for Get Spline.
- From Get Spline, drag out and search for Find Location Closest to World Location.
- For the World Location input, use Get Actor Location (to get the player’s current position).
- Set Coordinate Space to World.
2.4 Smooth Camera Movement
To make the camera movement smooth, we’ll use the VInterp To node.
- From Follow Camera, drag out and search for Get World Location.
- Connect this to a VInterp To node.
- For the Target of VInterp To, use the result of Find Location Closest to World Location.
- For the Delta Time, use Get World Delta Seconds.
- Set the Interp Speed to
2
(you can adjust this to control the speed of the camera). - Connect the output of VInterp To to the New Location input of Set World Location.
Now, the camera will smoothly interpolate between its current location and the location on the spline closest to the player.
Step 3: Camera Settings
We need to adjust the camera so that it no longer follows the player’s control rotation.
3.1 Disable Camera Rotation Inheritance
- Select your Follow Camera in the Components tab of the Character Blueprint.
- In the details panel, uncheck Use Pawn Control Rotation, Inherit Pitch, Inherit Yaw, and Inherit Roll.
3.2 Change Camera Location Mode
- Change the Location Rule of the camera from Relative to World.
Click Compile and Save.
Step 4: Set Up the Camera Spline in the Level
4.1 Add the Spline to the Level
- Open your level and drag BP_CameraSpline into the scene.
- Adjust the spline’s points to define the path for the camera.
- To add more points to the spline, hold down
Alt
and drag from an existing point. - You can adjust individual points by selecting them and moving them around the level.
- To add more points to the spline, hold down
4.2 Fine-Tune the Spline
- You can also adjust the smoothness of the spline by selecting the individual points and manipulating their tangents (the control handles that appear on the spline points).
Step 5: Test the Camera
5.1 Run the Game
- Hit Play in the editor.
- Move the player around and observe how the camera smoothly follows the spline path, adjusting based on the player’s position.
You can customize the spline to fit your level design, making the camera follow different curves, go up and down, or even follow complex routes that the player can’t reach directly.
Leave a Reply