Camera2D stops player from moving

Godot Version

`4.3

Question

I am trying to learn how to use godot using a very, very simple tutorial. I have my player in the scene and he can move but whenever i add camera2d as a child node of the player, the zoom happens but the movement stops. My movement code is as follows:

extends CharacterBody2D


func _physics_process(delta):
  
	velocity = Vector2()
  
	if Input.is_key_pressed(KEY_LEFT):
		velocity.x -= 1
	if Input.is_key_pressed(KEY_RIGHT):
		velocity.x += 1
	if Input.is_key_pressed(KEY_UP):
		velocity.y -= 1
	if Input.is_key_pressed(KEY_DOWN):
		velocity.y += 1
	
	velocity *= 50  
	
	move_and_slide()

The screenshot of my godot screen is as follows:

Any help will be highly appreciated!

1 Like

I got it to move, but the problem now is that any child node of the player master node also moves with the player though the player GDScript is attached to the player only!

1 Like

Everything thats a child of the player will also be moved with it. if you dont want that you happen you have to make it either a sibling or a parent of the player

my present node setup is as follows:

image

clearly coin is a sibling of the player, but when the player moves, the coins move too, though they have separate scripts.

Please share your updated physics function

the coin script:

extends Area2D


# Called when the node enters the scene tree for the first time.
func _ready() -> void:
	pass # Replace with function body.


# Called every frame. 'delta' is the elapsed time since the previous frame.
func _process(_delta: float) -> void:
	pass


func _on_body_entered(body):
	body.scale.x += 0.2
	body.scale.y += 0.2
	queue_free() # Replace with function body.

The player script is:

extends CharacterBody2D


func _physics_process(_delta):
  
	velocity = Vector2()
  
	if Input.is_key_pressed(KEY_LEFT):
		velocity.x -= 1
	if Input.is_key_pressed(KEY_RIGHT):
		velocity.x += 1
	if Input.is_key_pressed(KEY_UP):
		velocity.y -= 1
	if Input.is_key_pressed(KEY_DOWN):
		velocity.y += 1
	
	velocity *= 50  
	
	move_and_slide()

The camera script is:

extends Camera2D
class_name CustomCamera2D

# Called when the node enters the scene tree for the first time.
# Replace with function body.


# Called every frame. 'delta' is the elapsed time since the previous frame.



 
# Target node the camera is following
@export var TargetNode : Node2D = null
func _process(_delta: float) -> void:
	set_position(TargetNode.get_position())

		 # Change this code to make custom camera movement

What is your target node in camera script

the player node

if its player node then it should work properly. I thought you were setting your main scene as target node.
Now, i have no idea what’s happening.

FYI, i was earlier using the Code Blocks visual-scripting interface which i then disable. Should i start over with a fresh project?

If your project is not too big, then you can try with a new project. I think it will be a good idea

things got weirder! I am following this tutorial:

with the example they provided, it is the coin that is moving ! i am laughing with grief! :joy:

Hmm… can you send your project by creating a zip, i would like to run it on my system

here it is

1 Like

@vivekchakraverty Ok its working now and it was quite simple actually
In camera script replace set_position(TargetNode.get_position()) to set_global_position(TargetNode.get_global_position())

And to clearly see it working add a background.

Here in this video, i am using screenshot of my game, so ignore large player and healthbar :sweat_smile:

its still not working in my project, maybe i need to reinstall godot as i previously installed orchestrator and godot-block-coding plugins. You can find the video here:

That is working perfectly.
Your camera is keeping the player at center that’s why it seems like coin is moving but actually your player is the one moving.

As I said just add a sprite as background to see it clearly.

Yeah! It’s working now!!! :slight_smile: , still no collisions

1 Like

@vivekchakraverty Connect body_entered signal of coin to coin_script for detecting player collision.

I did that with this code:

func _on_body_entered(body):
	body.scale.x += 0.2
	body.scale.y += 0.2
	queue_free()# Replace with function body.

but it isnt producing the desired results! :frowning_face: