Godot Version
Godot v4.6.2
The camera2d of my game is not centered on the character body
So I’ll be direct. I’m making a top down view game, i know there was once a discussion about this, but it didnt help. So i have my character scene, which is a characterbody2d node, i have the collision shape node and the animated sprite node and the camera 2d node(this last three are childs of the characterbody2d), every single one of this nodes have their transforms in (0,0). and the player can visually move around the tile map without the camera being centered on it.
I do have a camera script, but the use of it was to controll a slight offset. Keep in mind this behaviour of the player not being centered, happens even if the script is not attached to the camera, so its not related to it.
extends Camera2D
@export var look_ahead_distance: float = 80.0
@export var mouse_influence_distance: float = 40.0
@export var follow_speed: float = 5.0
@export var return_speed: float = 3.0
@export var dead_zone: float = 20.0
@export var base_offset: Vector2 = Vector2.ZERO
var target_offset: Vector2 = Vector2.ZERO
@export var player: CharacterBody2D
func _process(delta: float) -> void:
print(player.global_position)
print(global_position)
if not player:
return
global_position = player.global_position
var velocity:= player.velocity
var new_target := Vector2.ZERO
if velocity.length() > dead_zone:
new_target += velocity.normalized() * look_ahead_distance
var mouse_vec: Vector2 = get_global_mouse_position() - player.global_position
var mouse_strength: float = clamp(mouse_vec.length() / 200.0, 0.0, 1.0)
var mouse_dir: Vector2 = mouse_vec.normalized()
if mouse_dir.length() > 0.1:
new_target += mouse_dir * mouse_influence_distance * mouse_strength
if velocity.length() <= dead_zone:
target_offset = target_offset.lerp(Vector2.ZERO, return_speed * delta)
else:
target_offset = new_target
offset = offset.lerp(base_offset + target_offset, follow_speed * delta)