Top Down Camera

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By StrikerSVX

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.

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:
		apply_camera_x()
		apply_camera_y()

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
	else:
		$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
	else:
		$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…

StrikerSVX | 2020-09-11 02:16

:bust_in_silhouette: Reply From: StrikerSVX

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:
		apply_camera(delta)

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.

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

minh_khoi | 2021-06-01 14:46

Your hard work is worth it

cookieoil | 2021-09-17 13:38