Platform Player
Using Unity 2021.3.27f1 and Visual Scripting 1.8.0. The project is using the 2D Core template.
Download the graph: PlatformPlayer.asset
A minimal player controller script for simple platforming. It supports walking on slopes and jumping.
Most of the complexity in this graph comes from better handling slope movement and jumping. It is possible to extract some more simple ideas from this graph, like how to orient the game controller input to the camera.
However, I think this can serve as a decent starting point for building your own player controller.
Player setup
The player is set up with a Capsule Collider
and a Rigidbody
. Freeze Rotation is enabled on all three axes. For smoother movement, interpolation is enabled as well.
Variables
A few variables are used to store the current state of the player controller. An important note is that the player will only be considered grounded on objects that match the groundLayer
- this can be set to multiple layers, but I do recommend to separate at least walls and ground.
Graph
I have separated the graph into three images, though they are all part of the same script graph.
On Start
On Update
On Fixed Update
In order to calculate the slope direction in accordance with the game controller input, we need to perform a number of steps.
- Create a vector from the controller input and clamp it to
1.0
. - Rotate the input around the Y-axis to match the camera’s view.
- Get the ground’s normal vector from the raycast.
- Calculate a direction along the ground/slope that keeps the XZ-direction but modifies Y. For this, we can use the equation:
- Normalize the direction and use the input magnitude and
moveSpeed
to determine to velocity along the ground.