The character trembles when turning

Godot Version

4.2.2

Question

Hello everyone again, while moving and simultaneously turning the camera, the character is twitching like this, please help

Video

Code

extends CharacterBody3D

const JUMP_VELOCITY = 4.5

# Get the gravity from the project settings to be synced with RigidBody nodes.
var gravity = ProjectSettings.get_setting("physics/3d/default_gravity")
var mouse_sens = 0.1
var running = false
var SPEED
var walk_speed = 5
var run_speed = 7

[video](https://imgur.com/XuxdgJJ)

func _ready():
	Input.set_mouse_mode(Input.MOUSE_MODE_CAPTURED)
	
func _input(event):
	if event is InputEventMouseMotion:
		rotate_y(deg_to_rad(-event.relative.x * mouse_sens))
		$visuals.rotate_y(deg_to_rad(event.relative.x * 0.1))
		%camera_mount.rotate_x(deg_to_rad(-event.relative.y * mouse_sens))
		%camera_mount.rotation.x = clamp(%camera_mount.rotation.x, deg_to_rad(-45), deg_to_rad(45))
		
	if Input.is_action_just_pressed("Esc"):
		if Input.get_mouse_mode() == Input.MOUSE_MODE_CAPTURED:
			Input.set_mouse_mode(Input.MOUSE_MODE_VISIBLE)
		elif Input.get_mouse_mode() == Input.MOUSE_MODE_VISIBLE:
			Input.set_mouse_mode(Input.MOUSE_MODE_CAPTURED)
			
func _physics_process(delta):
	if Input.is_action_pressed("Shift"):
		SPEED = run_speed
		running = true
	else:
		SPEED = walk_speed
		running = false
	
	# Add the gravity.
	if not is_on_floor():
		velocity.y -= gravity * delta

	# Handle jump.
	if Input.is_action_just_pressed("Space") and is_on_floor():
		velocity.y = JUMP_VELOCITY

	# Get the input direction and handle the movement/deceleration.
	# As good practice, you should replace UI actions with custom gameplay actions.
	var input_dir = Input.get_vector("A", "D", "W", "S")
	var direction = (transform.basis * Vector3(input_dir.x, 0, input_dir.y)).normalized()
	if direction:
		if running:
			if %AnimationPlayer.current_animation != "run":
				%AnimationPlayer.play("run")
		else:
			if %AnimationPlayer.current_animation != "walk":
				%AnimationPlayer.play("walk")
				
		$visuals.look_at(position + direction)
		velocity.x = direction.x * SPEED
		velocity.z = direction.z * SPEED
	else:
		if %AnimationPlayer.current_animation != "idle":
				%AnimationPlayer.play("idle")
		velocity.x = move_toward(velocity.x, 0, SPEED)
		velocity.z = move_toward(velocity.z, 0, SPEED)

	move_and_slide()

Do you rotate the 3D model with visuals.look_at() ?

If yes, you could try doing it in _process() instead of _physics_process().
Physics processing doesn’t tick at the exact same rate/time than rendering (framerate), so that could explain the twitching.

Otherwise, I’m not sure it’s normal that you rotate the visuals in both _input() and _physics_process().

1 Like

thanks for the help, I rewrote the code a little bit, and did as you advised, everything works

1 Like

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.