Godot Version
4.4.1.stable
Question
Hi, total noob here. I just started yesterday and I cannot have smoothed scrolling without the character jittering around. I've tried numerous solutions but none of them have worked, and I'm at my wits' end. Please, is there something I'm missing?
Character code:
extends CharacterBody2D
var char_direction: Vector2
const grid_size = 16
var is_moving = false
var next_destination: Vector2 = position
#movement
func _physics_process(delta: float) -> void:
#func fix_jitter():
#if not $PlayerStateMachine.is_moving():
#position = position.round()
position = next_destination
char_direction.x = Input.get_axis("move_left", "move_right")
char_direction.y = Input.get_axis("move_up", "move_down")
move()
if char_direction:
velocity = char_direction * delta
if char_direction.x > 0:
%player_sprite.flip_h = false
%player_sprite.animation = "walking_side"
elif char_direction.x < 0:
%player_sprite.flip_h = true
%player_sprite.animation = "walking_side"
elif char_direction.y > 0:
%player_sprite.flip_h = false
%player_sprite.animation = "walking_down"
elif char_direction.y < 0:
%player_sprite.flip_h = false
%player_sprite.animation = "walking_up"
else:
velocity = velocity.move_toward(Vector2.ZERO, 0)
if %player_sprite.animation == "walking_side": %player_sprite.animation = "idle_side"
elif %player_sprite.animation == "walking_up": %player_sprite.animation = "idle_up"
elif %player_sprite.animation == "walking_down": %player_sprite.animation = "idle_down"
move_and_slide()
#snap to grid
func move():
if char_direction:
if is_moving == false:
is_moving = true
var tween = create_tween()
tween.set_process_mode(0)
tween.tween_property(self, "next_destination", position + char_direction*grid_size, 0.1)
tween.tween_callback(move_false)
func move_false():
is_moving = false
Camera code:
extends Camera2D
@export_category("Follow Player")
@export var player: CharacterBody2D
@export_category("Camera Smoothing")
#@export var cam_smooth: bool
@export_range(1, 10) var smooth_amount: int = 8
# Called every frame. 'delta' is the elapsed time since the previous frame.
func _physics_process(delta):
if player != null:
var cam_pos: Vector2
#if cam_smooth:
var weight: float = float(21 - smooth_amount) / 100
cam_pos = lerp(global_position, player.global_position, weight)
#else:
#cam_pos = player.global_position
global_position = cam_pos.round()
I have:
- Set camera process to physics
- Enabled physics interpolation
- Set physics to 60fps
- As the code shows, set the player and camera to both run on physics, and rounded the coordinates
- Anything else that people have said to do (as long as I could understand what they were talking about)
Please help