Camera working specific (Camera following to mouse from player)

Godot Version

4.2.1

Question

Hello! Im new in godot and trying make camera what following mouse, i research all browser and found only ± working code:

extends Camera2D

const camera_dead_zone = 20

func _input(event: InputEvent) -> void:
	if event is InputEventMouseMotion:
		var _target = event.position - get_viewport().size * 0.5
		if _target.length() < camera_dead_zone:
			self.position = Vector2(0,0)
		else:
			self.position = _target.normalized() * (_target.length() - camera_dead_zone) * 0.5

When they in Node2D - All ok, but when in player (CharacterBody2D), they getting be crazy.

Godot_v4.2.1-stable_win64_hNdHP0uKyc

Godot_v4.2.1-stable_win64_Pai91OS7zw

Your camera script is based around the center of the screen, but you have tried to center it around something else (a character at the bottom left). You will have to change your algorithm to include the player’s position and maybe even global coordinates.

extends Camera2D

const camera_dead_zone = 20

func _input(event: InputEvent) -> void:
	if event is InputEventMouseMotion:
		
		var _target = event.global_position - (get_viewport().size + self.global_position) * 0.5
		if _target.length() < camera_dead_zone:
			self.position = Vector2(0,0)
		else:
			self.position = _target.normalized() * (_target.length() - camera_dead_zone) * 0.5