I’m building a platformer demo for a small project and need to implement a max horizontal speed limit in order to fix some issues. I have zero code knowledge so I’m hoping someone here can write out the few lines (I hope) of script needed to achieve this.
I’m basing the demo off the Unity 2D Gamplay tutorial files, and the included character controller already limits vertical movement speed, but I’m unsure how to adapt it to horizontal movement. I’ve found the following lines in the script:
var maxFallSpeed = 20.0;
movement.verticalSpeed = Mathf.Max (movement.verticalSpeed, -movement.maxFallSpeed);
I can make a new variable easy enough (
var maxMoveSpeed = 20.0;), but I don’t know how to write a function for it to take effect.
Primarily this need to take effect while in the air, but it doesn’t matter if it takes effect while the player is grounded as well.