Created
December 17, 2015 05:34
-
-
Save FEDE0D/8825a0ba2666c37ab27f to your computer and use it in GitHub Desktop.
Vector normalized in Godot
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
1º Add the direction vectors. Ie: you want to add the right key + up key velocity. | |
var vel = vel_left + vel_right | |
2º Normalize the velocity, this will make the length of the vector exactly 1 unit. | |
vel = vel.normalized() | |
3º Now the movement would be only 1 unit per frame, so multiply for the real speed | |
vel = vel * speed | |
translate(vel * delta) | |
4º That will make the movement of the body constant (no drag). But you can normalize the vector only when the speed it's too high: | |
var vel = vel_left + vel_right | |
if vel.length() > MAX_VEL: | |
vel = vel.normalized() * MAX_VEL | |
translate(vel * delta) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment