Good day. Let me introduce myself, for those who don’t know me yet. My name is Dima. I have been working as a C++ developer for over 5 years. Currently I work in a large Gamedev studio. Outside of work, I enjoy creating educational content for YouTube and Twitch channels.
In my small pet project, I was inspired by the idea to implement my own parkour system.
What is parkour? Parkour – high-speed movement and overcoming obstacles using jumping elements. The main problem that needs to be solved: the character must be able not only to jump and run, but also to interact with obstacles. The best example of parkour in 3rd person games is the Assassin’s Creed series, and in first person games it is undoubtedly Mirror’s Edge. I will be inspired by the latter. I’ll look at the solution I’ve come to so far and what problems I’ve solved.
Note: this post is a slightly modified version of my own article from the DTF website for local format. There is no way to post the program code in a readable form on Stopgame, so links to pastebin.com
What is an obstacle?
First of all, let’s decide what we will perceive as an obstacle with which the player can interact. After some thought, I settled on parallelepipeds whose rotation around the OX and OZ axes is 0. That is, these are parallelepipeds whose lower and upper faces are strictly parallel to the surface of the “floor”. They should not be perceived as a set of abstract cubes on which the main character will climb. By customizing their scale, they can be used to make a Mario platform, a flat billboard, ventilation system pipes, and so on.
What is our goal?
At the moment, I settled on https://betsensecasino.co.uk/ the simplest one – instead of trying to jump on an obstacle, as in old-school games, the character should try to climb onto it, which will be accompanied by a special animation.
Breaking down an obstacle into its components
For each obstacle, I identified 5 main elements: four walls and the top edge. Walls – this is the part we will climb, top edge – target. In order to break the obstacle, we turn to the component BoxCollider. Using its dimensions, as well as the scale of the object itself, its rotation and coordinates, we calculate 8 corner points of the parallelepiped in the world coordinate system. Let’s move on to the implementation of the method.
Implementation of dividing obstacles into walls
Let’s analyze the class ParkourObstacle. The main task of this class is to store a set of walls and the top face, and also be able to determine which wall the point passed to it belongs to. Let’s look at the implementation; I will exclude elements like properties and constructors at this stage so as not to distract from the essence. The full code will be provided at the end of the article.
Control
All preliminary operations have been completed. Now you can implement the character’s behavior. It’s hard to call it control, more like determining the moment when you need to start parkour. I will not focus on my own implementation of the first-person control script. It has some shortcomings and I don’t think anyone can be surprised by another FPS controller. If you are interested in its implementation, write in the comments, I will write an article. I put all the logic of parkour into a separate script, I won’t say that it will easily and simply fit any first-person control script. However, with minor changes in it and in your control logic, you can take it into service.
In a nutshell, the conditions for launching parkour can be described as follows:
I think it’s worth explaining only the first condition. Obstacles available for attempt will be determined using Capsule Collider, which will be a trigger collider. That is, it will only determine the entry and exit of other colliders using the functions OnTriggerEnter And OnTriggerExit:
Next in the logic frame, checking all these conditions and identifying obstacles for parkour(Physics.Raycast from the character’s position, not the camera, facing forward), we make sure that the distance to the top edge of the obstacle does not exceed the permissible limit (a configurable parameter), and after that we run the climbing algorithm:
climbing
I divided the process of climbing an obstacle into three stages:
Why is the transition needed?? In most cases, the starting point for climbing will not coincide with the character’s position in space, so the character must be smoothly moved and turned to face the wall. The moving speed will be a configurable parameter. This is followed by lifting up and moving forward. This code will be executed within one Coroutines.
The logic fits into three methods:
StartClimbing — Calculation of starting points, disabling control, starting climbing, maintaining the character’s speed before starting parkour
Conclusion
Our character learned to determine that there is an obstacle, break it down into its component elements and climb onto them. I’m currently working on the wall running mechanics. Thank you for your attention, write your wishes and questions in the comments. Follow developments on Youtube. Full development, detailed explanations and demonstration of work can be found in this video:
No Comments