Beginner looking for 2D Camera setup and movement tutorials

StapperStapper Posts: 2Member

Hi all,

I'm a complete beginner to Godot and looking to find some tutorials to setup my camera and letting the player move it around. It seems that my google skills are failing me as I can only find tutorials on how to attach the camera to a player character. Since my game won't have a character, I'm not sure how to set everything up. I have a wide level and would like the player to be able to scroll through the entire level, both by clicking and dragging and using the keyboard.

Basically I'm looking for tutorials on how to:
1. Setup the camera in my scene's node structure
2. Let my HUD move with the camera
3. Let the player move the camera left and right with the keyboard
4. Let the player move the camera left and right by clicking and dragging
5. Stop the camera at the end of the level borders

Thanks for reading!

Comments

  • TwistedTwiglegTwistedTwigleg Posts: 3,525Admin

    I don’t have a tutorial that covers all of those topics, but here’s what I have found:

    As for the other questions:

    1. Let my HUD move with the camera

    If you place your hud as children of a CanvasLayer node, then they should stay in the same position relative to the camera. That said, I have not done very much Godot 2D work (especially not recently) so I might be misremembering.

    1. Let the player move the camera left and right with the keyboard

    As long as you are using a Camera2D node, to move the camera all you need to do is change the position of the Camera2D node. Something like the code below should work if attached to the Camera2D node (untested):

    extends Camera2D
    const MOVE_SPEED = 100
    func _process(delta):
        if Input.is_action_pressed(“ui_left”)):
            global_position += Vector2.LEFT * delta * MOVE_SPEED
        elif Input.is_action_pressed(“ui_right”)):
            global_position += Vector2.RIGHT * delta * MOVE_SPEED
    
    1. Let the player move the camera left and right by clicking and dragging

    This would work similarly to the code I gave in answer 3, but you will need to use the _input function and InputEvents. It shouldn’t be too hard, but you will probably need to look at tutorials showing how to handle and process InputEvents in Godot, like this tutorial about Input in Godot from GamesFromScratch.


    Hopefully this helps answer some of your questions
    (Side note: Welcome to the forums)

  • StapperStapper Posts: 2Member

    Oh wow, I wasn't expecting such a detailed answer!

    It seems so easy at first glance. I'll check all of it out thoroughly and apply it.

    Thank you so much!

Leave a Comment

BoldItalicStrikethroughOrdered listUnordered list
Emoji
Image
Align leftAlign centerAlign rightToggle HTML viewToggle full pageToggle lights
Drop image/file