Splines and Moving Platforms
The Brief
For this task I was required to build a spline for the follow, this creates a 2.5D effect and is commonly used in 3D platformers. Whilst the player is walking along the spline their movement should be limited to only two directions as well as the ability to jump. For this effect to work properly I should also adjust the follow camera’s veiw and behaviour. I was also required to build a moving platform sequence as a challenge for the player, the plaforms should follow a spline in the same way the player did. To achieve this I must make a robust spline system that is reusable troughout my project. Some extra things I aim to achieve to make a more polished system are as follows:
- A smooth camera transition when the player enters and exits the spline.
- The spline should have curves rather than just straight lines.
- The moving platforms should act to the player’s weight.
- A respawn system that is convient for the player if they fall off a platform.
- Polish the platforming so it is fun and challenging for the player.
A screen shot of a platforming scene from the game Malice.
Implementing the spline for the player.
Creating a spline for the player to follow led me to read around the topic Bezier curves. I found an awesome video tutorial series expaining the topic by Sebastian Lague. If you don’t have the time to watch this series I’ll do my best with a breif explanation of what a Bezier curve is.
Moving from one point to another linearly is known as lerping, the following equation can be used to find a location between these points.
p1 + (p2- p1) * t, where t is any given value between 0 and 1.
Moving from one point to another in a Bezier curve requires two more points to be added along a line. When interpolating between each of these points a cubic curve is formed, the value of t can be varied to change the shape of this curve. The following equation defines the Bezier curve.
p1 * (1 - t)^3 + p2 * t * 3(1 - t)^2 + p3 * t^2 * 3(1 - t) + p4 * t^3
After following the whole tutorial series I was able to implement this into Unity. It meant I could make objects to follow a Bezier curve of any size at any given speed. The result of the character following this a curve, or ‘spline’, can be seen below.
Creating the platforming sequence.
My platforming sequence is built up of the cutscene system that I produced in my Switches, Doors and Follow Camera blog post, along with the spline system I used previously. I made the platforms follow a spline in the same way the player did, the only difference being they travel at constant speed rather than taking controller input. I instantiated each platform into the world using an animation I put together, at the end of this animation the platform would begin to follow the spline.
A clip showing the cutscene before the platforming sequence.
The player is made a child of the platform whenever the character is colliding with that platform instance. This allows the player to inheret the position of the platform and travel along with it without having to give any movement input. I initially used Unity’s Rigidbody component to simulate the platform’s physics when the player interacted with it, but balancing on it proved to be frustrating and wasn’t a fun experience. I then decided to make my own physics system, which meant the platform would tilt on a single axis, relative to the player’s position on the platform. This made the platforms more exciting to interact with, giving the player a good chance to counter act the current rotation of the platform. The result can be seen below.
Finally I added respawn points around the platforming area, meaning failing to make a jump was much less frustrating. This system will initially require a designer to create game objects with a trigger volume that can be added to an array of ‘respawn locations’. Each time the player enters one of these volumes, that game object will become the active respawn location. All the features are demonstrated in the video below. Take note that I will mention how I implemented the enemy and combat in my next delvog, Melee Combat.