My camera 2d freezes my player when it becomes child of it

Godot Version

4.5.1 stable

Question

So I have been trying to make this automation game thats 2d and I want the camera to follow player but when I do that the player freezes and cant move.

this is my code for player (the project is new and Im a beginner)

extends CharacterBody2D

#region Variables

@onready var pickaxe = $Pickaxe
@onready var player = $AnimatedSprite2D

const SPEED = 60.0
const JUMP_VELOCITY = -200.0
const floatiness = 0.8
#endregion

#region Functions

func _pickaxe():
	if Input.is_action_pressed("Piackaxe"):
		pickaxe.show()
	else:
		pickaxe.hide()
	
func _gravity(delta):
	if not is_on_floor():
		velocity += get_gravity() * delta * floatiness
		
func _handleMovement():
	var direction := Input.get_axis("left", "right")
	if direction:
		velocity.x = direction * SPEED
	else:
		velocity.x = move_toward(velocity.x, 0, SPEED)

func _handleJump():
	if Input.is_action_just_pressed("jump") and is_on_floor():
		velocity.y = JUMP_VELOCITY

func _handlePlayerAnimation():
	if velocity.x != 0:
		player.play("Run")
	else:
		player.play("Idle")
		
func _Handleflip():
	var direction := Input.get_axis("left", "right")
	if direction > 0:
		player.flip_h = direction < 0
		pickaxe.scale.x = 5
		pickaxe.position.x = abs(pickaxe.position.x) 
	elif direction < 0:
		player.flip_h = true
		pickaxe.scale.x = -5
		pickaxe.position.x = -abs(pickaxe.position.x) 
		
	
#endregion

#region  calling functions

func _physics_process(delta: float) -> void:
	
	_gravity(delta)
	_handleJump()
	_handleMovement()
	_Handleflip()
	_handlePlayerAnimation()
	_pickaxe()
	move_and_slide()
	
#endregion

How do you verify that it freezes the player?
If your camera is a child of the player, they will move exactly in sync, which may look like nothing is moving on the screen if you have no other point of reference. Do you have some environment elements in the scene that you can verify the Player’s movement against?

3 Likes

the problem is that it works when camera is not the child but when I put it as a child the player cannot move. when I put position smoothing the player can move a bit but then the camera will pull him back my camera zoom is 2

Read again what @wchc said. You need a point of reference. If the camera is parented to the player, they’ll move together so player will appear stationary relative to the camera. If there is no other graphics in the scene it’ll look like nothing is happening. Add some background sprites to the scene.

1 Like

Or print the player position to see if it changes.
Or look at the position at runtime in the inspector.

1 Like

thanks

It worked thank you for your help!!!

the background was a game changer!!!

1 Like

When you made the camera follow the player, did you add a Camera2D as a child of the player and just enable it, or did you change something in the movement code?