Stairs and Lerp - 3D FPS in #gamemaker
Gaming
Introduction
In the world of video games, stairs can present a unique problem for developers. When we consider how characters move, it becomes evident that they don't actually walk on the ground but rather glide over it. This creates a challenge when a player glides into an elevation, like a step. To distinguish between a wall and a step, developers need to implement some form of logic.
In a previous development log, collision code was established to handle walls during jumping and falling. The next step is to address the question: how do we determine whether a collision is with a stair or a wall? The solution can vary depending on the game, but one common approach is to make stairs into invisible ramps. By converting stairs into inclines, players can glide up seamlessly. However, this method has several drawbacks. It requires extra levels of design complexity, as every staircase and minor elevation would need to be converted into ramps. Additionally, it creates a disconnect between the visual model and the underlying collision detection.
A more efficient approach is to set a maximum step size. This value dictates how high a character can step up, effectively allowing the game to differentiate between a step and a wall. When a player collides with an object, the game checks if moving up by the specified step size results in the character no longer colliding. If this is the case, the game moves the character up accordingly.
However, an issue arises with this snapping effect, where the character's movement may appear jittery due to the set amount of upward displacement. A more refined technique would involve using vectors or raycasting to ensure players smoothly transition to the new height. An alternative solution is to apply linear interpolation (commonly known as lerp) to the camera height, which provides a smoothed transition.
What is Linear Interpolation?
Linear interpolation (lerp) is a method used to smooth out changes by gradually transitioning a value from one point to another. To apply lerp, three values are necessary:
- Value A: The current value.
- Value B: The target value.
- Lerp Amount: Typically a percentage between 0 and 1.
For example, if Value A is 0, Value B is 100, and the lerp amount is 0.3, the lerp function would return 30. This means that rather than abruptly changing the camera's height, the game can incrementally adjust the height over time, making for a smoother transition.
In summary, when a player collides with an object, the game checks if moving up by the defined step height resolves the collision. If it does, linear interpolation is then utilized to adjust the camera height smoothly, eliminating the snap effect. This provides the functionality needed to handle stairs within the game smoothly.
Now that we have established a collision system capable of managing player actions such as walking, jumping, falling, and climbing stairs, there is but one challenge left to conquer—the ability to swim!
Keyword
- Stairs
- Lerp
- 3D FPS
- Collision detection
- Maximum step size
- Linear interpolation
- Game development
- Camera height
- Glide movement
FAQ
Q: What is the main issue with stairs in video games?
A: The main issue is that characters glide over surfaces rather than walking, complicating how they interact with elevations like stairs as they may not distinguish between walls and steps effectively.
Q: What is one method developers use to handle stairs?
A: Developers often convert stairs into invisible ramps, allowing characters to glide up inclines instead of stepping, although this approach can increase level design complexity.
Q: How does setting a maximum step size help in collision detection?
A: Setting a maximum step size allows the game to determine how high a character can step, differentiating between a step and a wall when collisions are detected.
Q: What is linear interpolation (lerp)?
A: Linear interpolation (lerp) is a method used to smoothly transition between values over time instead of making an abrupt change, which can help avoid snapping effects in camera height.
Q: How does the lerp function work?
A: The lerp function requires three values: the current value (A), the target value (B), and a lerp amount (a percentage). It calculates an intermediate value based on these inputs to create a smooth transition.