Hello. I came across some strange behavior in Godot. I believe it’s a bug in the engine, but if I’m wrong and this is intended behavior, please correct me.
Description of the glitch
There’s nothing in my project that should let the player to move at different speeds. But when I run the project and move the sprite with arrow keys (left and right), I see that the speed of the sprite is inconsistent.
When I first start moving (in any direction), I move fast. But when I switch direction, I move about 50% slower for some amount of time. The amount of time I move slower seems to be determined by how long I traveled before I turned around. After moving slowly long enough, I eventually start moving fast again. But if I then turn araund, I’ll notice the character moving slow in the new direction.
The glitch is seemingly caused by rounding. The glitch is not exclusive to move_and_slide() based movement. The glitch only happens when the movement speed is set to exactly 90.
How to reproduce it? (you should try it)
-
Make a new project. Add a CharacterBody2d node and give it a sprite2d. You may drag the node to the center of the camera so it’s easier to see.
-
Add a script to the CharacterBody2d and paste this code:
extends CharacterBody2D var speed: int = 90 # The glitch seems to only happen at exactly 90 speed. func _physics_process(delta): position.x += Input.get_axis(“ui_left”, “ui_right”) * speed * delta # Make movement happen position = position.round() # We round the position. This line seems to cause the glitch.
That’s it. Now run the project and move around. You’ll understand what I’m talking about very quickly.
My Godot version: v4.6.3.stable.official [7d41c59c4]
