Jittering/Stuttering with movement.

Godot Version

4.1.3

Question

I had a project with movement that worked fine but suddenly started jittering/ stuttering. I created a brand new project because I couldn’t figure out the problem just to find the same problem in the brand new project. It is with the Godot movement that comes with the script.

The weird thing is that the movement smooths out when I press another button. I’m not allowed to upload a video because I’m a new user.

Please help!

I recommend we start with a pic of the scene tree.

debug

This is the scene tree for the player. The Area 2D is so that I can detect when the body is entered. Not sure if that signal is built into the CharacterBody2D node tho will have to read up on that.

No cameras?

No its just a basic Node2D scene as the main and then the player in the main scene.

No scrolling whatsoever? Then we’ll need to see the movement script.

extends CharacterBody2D


const SPEED = 300.0

# Get the gravity from the project settings to be synced with RigidBody nodes.
var gravity = ProjectSettings.get_setting("physics/2d/default_gravity")


func _physics_process(delta):
	# Get the input direction and handle the movement/deceleration.
	# As good practice, you should replace UI actions with custom gameplay actions.
	var direction_x = Input.get_axis("ui_left", "ui_right")
	if direction_x:
		velocity.x = direction_x * SPEED
	else:
		velocity.x = move_toward(velocity.x, 0, SPEED)

	move_and_slide()

I have tried many different ways of movement but this is just the default code Godot gives with the CharacterBody2D

Nothing obvious seems to pop up. Try adding delta to both velocity calculations. SPEED*delta on both. Might have to also increase speed, maybe.

Thank you for all the help but this still does not seem to solve the problem. I am really confused as to what is causing this and I have been busy trying to fix this for weeks now but still cant seem to find a solution. Thanks again!

Your code should work fine. Try replacing if/else statement with:

velocity.x = lerp(velocity.x, direction_x * SPEED, 0.99)

it should give similar results.
Also check if you have connected controllers, they might have dead zone jitter.
Might be v-sync problem, then try to change v-sync mode in project settings. Also try restarting PC (not turning off and on), updating video drivers.
If you editing in browser then stutters can be common and disappear/reappear by themselves.
Hard to figure out the cause not seeing jitter itself, but hope this helps.

Look at function move_toward().
If you take your variables and match to func - there is something crazy must happens. Looks like move_toward(0, 0, 300)
move_toward(FROM, TO, DELTA) - so…
move_toward(SPEED, 0, 150) = (300, 0, 150) = 300-150, = 150.
velocity.x = 150.

Thanks but this still does not seem to be working I played with the V-Sync settings but nothing changes, I also changed the code but It’s still the same.

I’m not sure what you mean by this comment?

can you post your video on yt and link it here maybe, itd prob be faster to solve that way