extends Camera2D
@export var player_node:Node
const CAMERA_MOVE_SPEED=100
const MAX_POS=50.0
const MIN_POS=-50.0
var current_pivot_position
var is_camera_moving:bool=false
var input=Vector2.ZERO
func get_input():
input.x=int(Input.is_action_pressed("Right"))-int(Input.is_action_pressed("Left"))
input.y=int(Input.is_action_pressed("Down"))-int(Input.is_action_pressed("Up"))
return input.normalized()
func _process(delta):
input=get_input()
if input==Vector2.ZERO and not Input.is_action_pressed("Right") and not Input.is_action_pressed("Down"):
is_camera_moving=false
current_pivot_position=Vector2.ZERO
position=current_pivot_position
else:
if not is_camera_moving:
is_camera_moving=true
current_pivot_position=position
current_pivot_position.x=clamp(position.x+input.x*CAMERA_MOVE_SPEED*delta,MIN_POS,MAX_POS)
current_pivot_position.y=clamp(position.y+input.y*CAMERA_MOVE_SPEED*delta,MIN_POS,MAX_POS)
position=current_pivot_position
remember to assign the player node
also put this camera as a child of the player
Also check this out, moving camera with middle mouse button grab: