move_and_slide()
returns a Vector3 which represents the linear velocity of your KinematicBody after the physics engine has moved it. If your character slides along a surface, this will affect the direction and length of the return value. This also means that if your body is walking into a wall, the length of the return value will be smaller than the velocity
parameter you passed in. Otherwise, if there were no obstacles along the way, the value returned by the method will be the same as the parameter that you passed in.
Previously, my Kinematic characters had issues with jitter because I didn't bother to cap their speed when attempting to move into corners, which led to the cycle that TwistedTwigleg described above. Fortunately, by using the return value from move_and_slide()
you can easily limit the body's speed without needing to do any special checks for collisions.
For this to work, your KinematicBody will need some sort of variable to keep track of the player's velocity. I'll call it velocity
in these examples. velocity
should at the very least be affected by the player's input, but you may want it to be affected by other things depending on what physics behavior you're going for.
After applying all the movements that you want to velocity
, all you have to do to prevent velocity
from exceeding what is physically possible is to set it to the return value of move_and_slide
like so:
velocity = move_and_slide(velocity)