skip to Main Content

Creating Flight Paths using Splines

In this post we’re going to create some flight paths for our character (similar to the World of Warcraft flight path system). We’re going to omit any UI interactivity for the sake of simplicity. If you would like to include some UMG behavior in the following tutorial, make sure to check out my UMG with C++ tutorial. But before we start, here is the end result:

What we’re going to create

Before we start typing any code, let’s explain what the above video demostrates. In this post, we’re going to create some Flight Stop Actors which have two spline components and two assigned float curves. The spline components describe the actual flight path that the character will follow when he selects either of them. As a convention in this post, one spline component corresponds to the next flight path while the other spline component corresponds to the previous flight path.

The assigned float curves describe the way that the character will travel the selected flight path (meaning the total travel time and therefore his travel speed).

The character contains a box component in order to identify any nearby flight stop actors. Moreover, I’ve provided two inputs in order to select the next or the previous flight path (if available).

In order to start implementiong the above mechanic, create a new C++ Third Person Template Project that comes in with UE4. In this post I’m using UE 4.13.1 so in case you’re using a different engine version you may need to edit the code a bit in order to match your API.

Creating the Flight Stop Actors

In order to create the Flight Stop Actor, create a C++ class that derives from the Actor class, name it FlightStopActor and add the following declarations:

Moreover, make sure to include the following header file:

#include “Components/SplineComponent.h”

Then, type in the following code into the constructor of your actor:

Save and compile your code. Then, create a Blueprint based on the above C++ class and assign a default static mesh. When you’re done with that, place some Flight Stop Actors in your level and extend their spline components. In my case, I’ve assigned a green color to the Previous Flight Paths and a blue-ish color to the Next Flight Paths.

Here is a screenshot of my map:

final_splines

Spline Components come with just two points inside the editor. In order to add more, select a point in your spline and click the “Alt” button while you’re moving the point. This way, you can create as many points you like.

Let’s move on to the character’s implementation.

Character preparation

Before we edit the code that comes with the Third Person C++ Project template, add two inputs for your project, named NextFlightPath and PreviousFlightPath. Then, open up the header file of your character and add the following code:

Before you continue any further, add the following includes right before the .generated.h library file:

#include “FlightStop.h”
#include “Components/TimelineComponent.h”

Switch up to your character’s source file. At the end of it’s constructor implementation, add the following code in order to initialize our box component:

Then, implement the following BeginPlay function and implement the following logic fo the OnFlightBoxColliderOverlap

When you’re done with that, add the following logic for the UpdateFlightTimeline function and ResetActiveFlightStopActor:

In case you’re interested more in what’s going on inside the UpdateFlightTimeline function, make sure to check out my tutorial on how to consume timelines using C++ here.

When you’re done with that, make sure to bind the action inputs that you have provided in the beginning of this section, using the following code inside the SetupPlayerInputComponent:

Then, provide the logic of the above inputs, using the following code:

In the UpdateFlightTimeline function, we declare that we want to play the provided timeline from start (line 14 in the provided code snippet). However, we need to explicitly tick the timeline inside the tick function. To do so, type in the following code:

Then, provide the following implementation for the TickTimeline function:

Save and compile your code. Then, make sure to provide timelines in each FlightStopActor. These timelines need to have an initial value of 0 and a target value of 1. The time value equals the total flight time so you can temper with that value to match your needs.

For example, here is the timeline that get’s played on the large spline component in the video shown above:

Click on image to enlarge in a new tab

Once you have provided the timelines, play with your character and once you get near a FlightStopActor press the keybind that you provided. In case your character won’t fly, make sure that your BoxComponent is aligned with your character (sometimes UE4 places these components by default in 0,0,0 – world space coordinates)

Update: Since a lot people ask me about the whole project, you can download it from here.

Avatar photo

This Post Has 9 Comments

  1. Can you post an example project with this code in use? I’m struggling to get my head around it a little bit.

    Thanks!

  2. can you post me ALSO the EXAMPLE PROJECT FILE
    I am also Greek my name is Vassilis

    POLY KALI DULIA!!
    ke ego o kaimenos polemao na matho UE4 alla pou…poli diskolo…….me Blueprint fisika apo programmatismo den skampazo tipota

    Thanks

  3. Hey im new. How to i just “Provide a timeline” ? I can add a timeline node in blueprint but then what? How can i make that timeline have anything to do with the c++ code?

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Back To Top