+1 vote

Hello, Striker here again, i'm doing a top down project that i hope to get finished unlike my other prototypes, i was looking around the web and i didn't find anything that could help me to solve the question of how would a top down camera behave, i was thinking of programming a way to keep the cursor and the player on screen and zoom out when the cursor goes farther away, but i can't see how it would/could be done, anyone has any idea of how this could be done? Or maybe some other design for a top down camera that isn't unfair to the player(s)? Because the game i'm making is to be somewhat of a top down shooter with local multiplayer/splitscreen.

in Engine by (99 points)

So since nobody said anything yet i made another attempt at making something that works the only problems is that my method still a little bit too clunky:

func _physics_process(delta):
    if is_under_player_control:

func apply_camera_x():
    var pos = global_position - get_global_mouse_position()
    if pos.x < (-get_viewport_rect().size.x + 600) or pos.x > (get_viewport_rect().size.x - 600):
        $Camera2D.global_position = $Camera2D.global_position
        $Camera2D.global_position.x = get_global_mouse_position().x

func apply_camera_y():
    var pos = global_position - get_global_mouse_position()
    if pos.y < (-get_viewport_rect().size.y + 420) or pos.y > (get_viewport_rect().size.y - 420):
        $Camera2D.global_position = $Camera2D.global_position
        $Camera2D.global_position.y = get_global_mouse_position().y

so what this code does is that it will take the position of the player and subtract the position of the mouse in the screen and if the mouse is outside the given parameters or the size of the screen in this case, the camera will stop and it will only return once the mouse returns inside of the given parameters, the only problem is that the camera movement is too fast for my likings and for some reason if you rotate the player (since the camera is a child of the player) it will get out of the range and you will have a hard time trying to get the player back in your view, i don't know how i would make this better but i hope this helps somebody to help me...

1 Answer

+1 vote

well, looks like i'm gonna have to anwser me by myself since i dicovered the way to do that
exactly the way i had in mind, this is how the code works:

var center = Vector2()
var damping = 12.0

func _physics_process(delta):
    if is_under_player_control:

func apply_camera(delta):
    var mpos = get_global_mouse_position()
    var ppos = global_position
    center = Vector2((ppos.x + mpos.x) / 2, (ppos.y + mpos.y) / 2)
    $Camera2D.global_position = lerp($Camera2D.global_position, center, delta * damping)

what this code does is that it gets the center between the player and the cursor and then half's the value of each vector, after that this code will make the camera position lerp between the global position of the actual camera and the new position (center) adding some damping for smoothing the movement.

by (99 points)

Thanks a lot! This was also what i was trying to do

Your hard work is worth it

Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read Frequently asked questions and How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to [email protected] with your username.