Then, when we modify our velocity, the new velocity will be 70% slower because we have modified the values of horizontal and vertical to be 70% of their value, so we will be multiplying our runSpeed value by either 0.7 or -0.7, again, setting out speed to 70% of runSpeed. This is why we use a combination of horizontal and moveLimiter. But if horizontal is -1, and we multiply that by 0.7, we’ll end up with -0.7, so we can move at 70% speed to the left.
It would always be positive, so you could only ever move right. Ironically in this case, multiplying horizontal by 0.7 will make horizontal equal to 0.7 or -0.7, but if we just said: horizontal = 0.7f MoveLimiter has the value of 0.7. So when we multiply any number by 0.7, that will give us 70% of that number. Our if statement checks if you’re moving diagonally – if horizontal and vertical are both not equal to 0, you’re moving diagonally. Next, make a new script and attach it to the character and add the following code inside your class (note there is an improved version at the end of this tutorial so if you’re just copy and pasting, scroll down first – although I strongly suggest you read the article to understand what it does): Rigidbody2D body float horizontal float vertical public float runSpeed = 20.0f void Start () First, attach a Rigidbody2D component to your 2D character, and turn the gravity scale down to 0.