- Home /
How to set a max horizontal speed, 2D platformer
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.
I think you should look at Vector3.Project documentation.
Answer by Seth-Bergman · Jul 23, 2012 at 06:15 AM
not familiar with the tutorial, but if you can find a var that applies to movement speed, you can add a line like:
if(speed > maxMoveSpeed)
speed = maxMoveSpeed;
in the line just before the line implementing the actual movement, which probably looks something like:
controller.SimpleMove(forward * speed);
or maybe:
transform.position = ....etc
if this doesn't do it, try posting the relevant code (that applies the movement)
Your answer
![](https://koobas.hobune.stream/wayback/20220613074545im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
The name 'Joystick' does not denote a valid type ('not found') 2 Answers
Character Controller Slide Action Script! 2 Answers
How to access Blend Tree "speed" in Mecanim from C# 0 Answers
Problem with time !! help me 1 Answer
Trying to rotate my characters upper body to face the mouses y position 1 Answer