CharacterBody2D is not in the centre of Camera2D after movement

Godot Version

4.4.1

Question

I am creating an orthogonal game. I have set up a CharacterBody2D. Set the limit dynamically to my TileMapLayer size multiply by the scale of the Node2D which is three. My issue is that I would like that my character is always be at the centre of the camera. But it does not. Camera is following my character but always at the bottom or at the right side of the screen. Initially it is centred.

Could you please point me to the right direction? Thanks a lot in advance! :slight_smile:

Is the camera a child of the characterbody2d?

Yes, it is. :slight_smile:

Can you show the code where you set the limits of the camera?

Yep! :slight_smile:

extends Camera2D

@onready var main_map: Object
@onready var nodeScales: Vector2
@onready var mapRect: Rect2i
@onready var tileMapSize: int
@onready var worldSizeInPixels: Vector2

func _ready() -> void:
	await get_tree().process_frame
	set_camera_limit()
	
func set_camera_limit() -> void:
	main_map = get_tree().get_nodes_in_group("GroundFloor")[0]
	nodeScales = main_map.get_parent().scale
	mapRect = main_map.get_used_rect()
	
	mapRect.size.x -= 2
	mapRect.size.y -= 2
	
	tileMapSize = main_map.rendering_quadrant_size
	worldSizeInPixels.x = mapRect.size.x * tileMapSize
	worldSizeInPixels.y = mapRect.size.y * tileMapSize
	
	limit_left = mapRect.position.x
	limit_top = mapRect.position.y
	limit_right = worldSizeInPixels.x * nodeScales.x
	limit_bottom = worldSizeInPixels.y * nodeScales.y

Tried to capture the issue. :slight_smile:
2025-03-30_08-49-44

Meanwhile I removed the scaling from Node2D & added zoom to Camera2D. Tried to play with view_port & global_position. I had no luck. :confused:

Hi!

I’d advice not having the camera as a child of your character. There are many reasons for that:

  • You may have to animate your character in some ways, changing its position or rotation, and you don’t want the camera to be affected.
  • You may want to make the camera follow the character with some smoothing, and having it dissociated.
  • You may want to lock your camera to some position or center it between many objects, and again, having it dissociated from one specific object may help.

About your issue, I’m not sure what’s causing it, but depending on what your character scene looks like, it may be related. I’d suggest you put your camera in a separate node, and add a player follow in the process function (you can start by just doing something like global_position = player.global_position, which will give the same result as if the camera was inside the character).
That way, the camera will be independent and you’ll be able to debug it more efficiently.

I know I’m not giving a ready-to-use fix to your problem, I wish I had one, but sadly I don’t :sweat_smile: I just hope that my suggestion may help, and if you implement it and it does not, feel free to ask again and share the updated code.

Hi @sixrobin, thanks a lot for your suggestion. I have tried this approach without any luck unfortunately. :confused:
It worked similarly as it was. Slightly worse, to be honest. :smiley:
So I guess, I have got a fundamental problem with my structure what I could not find and debug.

I managed to find a solution on reddit!
Please find my post there:
https://www.reddit.com/r/godot/comments/1jphocg/characterbody2d_is_not_in_the_centre_of_camera2d/