Camera2D problem: player can only move a little bit, other objects move in opposite direction

Godot Version



Hi, I’m having trouble with the Camera2D node in Godot. I attached a Camera2D to the player, but the player can only move a little bit, and any other object moves in the opposite direction. I tried disabling the drag margin and the camera limits, but it didn’t help. Does anyone know how to fix this?
Node Tree

Player Code:

extends CharacterBody2D

var canon_ball_scene : PackedScene = preload("res://scenes/projectiles/canon_ball.tscn")
var can_fire_main_canon : bool = true
var can_fire_side_canon : bool = true
const speed  = 400

# Called every frame. 'delta' is the elapsed time since the previous frame.
func _process(_delta):
	var direction = Input.get_vector("left","right","up","down")
	velocity = direction * speed
	var player_direction = (get_global_mouse_position() - position).normalized()
	if Input.is_action_pressed("primary_action") and can_fire_main_canon:
		var canon_ball = canon_ball_scene.instantiate() as RigidBody2D
		canon_ball.position= $Markers/CanonLooseMarkerC1.global_position
		canon_ball.linear_velocity = canon_ball.speed * player_direction
		can_fire_main_canon = false
	if Input.is_action_pressed("secondary_action") and can_fire_side_canon:

		var markers = $Markers.get_children()
		for i in range(6):
			var up_down_vector = Vector2.UP
			if i >= 3:
				up_down_vector = Vector2.DOWN
			var canon_ball = canon_ball_scene.instantiate() as RigidBody2D
			canon_ball.position = markers[i].global_position
			var ship_angle = rotation 
			var canon_ball_velocity = up_down_vector.rotated(ship_angle).normalized()
			canon_ball.linear_velocity = canon_ball_velocity * canon_ball.speed
		can_fire_side_canon = false

func _on_canon_timer_timeout():
	can_fire_main_canon = true

func _on_side_canon_timer_timeout():
	can_fire_side_canon = true

It depends on what you made, and what you want to acheive. I understand the problem, but you said nothing about the code or the node tree you have, and neither about what you want to see happen. It’s hard to guess!

1 Like

Oh, I should’ve done that. I want the camera to follow the player smoothly, and I’ve added the code and node tree to the post. Thanks

You most likely don’t want your camera inside your player object. Instead script it so it’s position matches the player position. To smooth it you can use lerp() like position = lerp(postion, player.position, 0.1)
Just store a reference to the object the camera is currently following in the camera script.