Camera2D stealing playermovement script

Godot Version

v4.3.stable.steam [77dcf97d8]

Question

Like others, I’'ve started learning Godot through Brackey’s tutorial. But I’ve run into a problem. For a while, everything worked ok; The camera stayed still as my character ran and jumped around. Then, after I placed down all my tiles, I tested the game again and found that somehow the script i’d latched onto the ‘player’ node had been stolen by the Camera2D node. The camera can ‘run’ and ‘jump’ around, but the character cannot. I tried deleting the player script, but even then the camera still moved around. I tried to make sure the player and camera2d nodes were absolutely seperate, I even tried deleting the camera node itself and re-adding it. I’m a total beginner, so maybe there’s something really obvious I’ve missed here, but I’m very much stumped. (By the way, the player script is just the default Godot 2d player movement. I’m following Brackey’s tutorial exactly.)

You’re providing a detailed description of what you’ve done to get to your current state. That’s not too important though. Saying that you’re following a particular tutorial is not really helpful either since beginners make a lot of errors that they are unaware of making.

Please provide a clear picture of how your project looks:

  • Relevant code
  • Node hierarchy / scene setup
  • Picture, GIF, or video of your unique problem that illustrates the current behaviour of your system.

You can’t expect someone to solve a technical issue if there’s no technical information.

1 Like

Ok! Let me try again:

Here is my node hierarchy (Game is in bold bc its the main node, everything else connects to it)

Game
Player (with movement script attached)
AnimatedSprite2D (child of player)
CollisionShape2D (child of player)
TileMap (Child of player)
StaticBody2D
CollisionShape2D (Child of StaticBody2D)
Camera2D

Sorry if this isn’t helpful either, im not used to formatting on this website!

The player node is the only thing with code attached to it, and here it is:

extends CharacterBody2D


const SPEED = 300.0
const JUMP_VELOCITY = -400.0

# Get the gravity from the project settings to be synced with RigidBody nodes.
var gravity = ProjectSettings.get_setting("physics/2d/default_gravity")


func _physics_process(delta):
	# Add the gravity.
	if not is_on_floor():
		velocity.y += gravity * delta

	# Handle jump.
	if Input.is_action_just_pressed("ui_accept") and is_on_floor():
		velocity.y = JUMP_VELOCITY

	# Get the input direction and handle the movement/deceleration.
	# As good practice, you should replace UI actions with custom gameplay actions.
	var direction = Input.get_axis("ui_left", "ui_right")
	if direction:
		velocity.x = direction * SPEED
	else:
		velocity.x = move_toward(velocity.x, 0, SPEED)

	move_and_slide()


I don’t think I can upload a video or picture of anything since my account is new

Oh and the ‘player’ is just a renamed CharacterBody2D, and the ‘game’ is a renamed Node2D.

Listen. Your whole issue is with a script being somewhere it shouldn’t be.

Please just take a screenshot of your scene hierarchy so that I can see where the script is located and how the hierarchy is built. A text description is not that digestible to an outside observer.
EDIT: I guess you’re a new user, so you’re prohibited from uploading a picture – damn. Perhaps use a list to visualize the hierarchy? Like this:

  • Game
    • Player (Script?)
      • AnimatedSprite2D
    • Camera2D
    • StaticBody2D

Does this mean that there is not any code on the camera, or?

I’m pretty confused right now.

Also, please use the </>-button for formatting your code on this forum. Most first-time users don’t know about this feature. Please edit your previous reply (with the pen icon) by selecting the lines of code and clicking the </> button and write GD after the first triple ''' to format it as GDScript code.

Ok, I edited my original post! And yes, there’s no script on the Camera, which is why I was so confused as to why it was ‘stealing’ (Using the script I put on the player object). The player is the only node with a script attached.

Thanks for fixing the formatting!

There’s nothing out of place in your script, and if the script is indeed where you say it is, the camera should not be moving.

I’m assuming the Tilemap you’re using is to display the terrain. Why would that be a child of the player? If you do that, the tilemap will follow the player. This may look as if the camera is moving when, in fact, the world is moving with the player.

Can you confirm that this is not the issue? Try printing the position of your player node and see what it says. Is the player moving, or is the camera moving?

print("Player position: %s" % global_position)
1 Like

This sounds like the camera isn’t moving the whole world is. The TileMap as a child of the player will move with the player, everything but the camera is moving in your game! Un-parent the TileMap from the player

1 Like

OH MY GOSH YOU’RE RIGHT! It seems so obvious now, and the player is moving as normal! Thank you! :smiley:

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.