Help With Peek Camera

Godot Version

3.5.3

Question

Hello!

So this week I am trying to create a camera that “peeks” in the direction of the mouse location. I’ve read over a few different ways that people are doing it, but none have seemed to fit the use case for me.

Here is a reference of what I am looking for.

And this is the code that I have that has worked out the best so far:

extends Camera2D

func move_camera():
	var mouse_pos = get_global_mouse_position()
	offset = mouse_pos / 4
	
func _physics_process(delta):
	move_camera()

The only problem with this code is that the further my player gets from the origin/spawn point, the further the camera becomes not centered on the player.

Any idea how to fix this issue?

Thanks!

P.S. I should note that I am a newbie at coding and game dev, so please bear with me!

It seems as if the camera position is in the mid-point between the character and the mouse position. You could optionally clamp the offset so it doesn’t go beyond a preset value.

How about something like this? (untested code below)

extends Camera2D

# set the character's node here so we can track its position
@export var target : Node2D  
@export var max_offset: Vector2

func move_camera():

	var mouse_pos = get_global_mouse_position()
	var target_pos = target.global_position
	offset = (mouse_pos  + target_pos) / 2  # midpoint between target and mouse

	# optionally, clamp the offset by some value
	offset = offset.clamp(-max_offset, max_offset)
	
func _physics_process(delta):
	move_camera()
1 Like

I thought it may have something to do with tracking the “target!”

The problem I am having now is an error that reads “Invalid call. Nonexistent function ‘global_position’ in base ‘Node2D’.”

Here’s my code

extends Camera2D

onready var target = $".."

func move_camera():
	var mouse_pos = get_global_mouse_position()
	var target_pos = target.global_position()
	offset = (mouse_pos + target_pos) / 2

func _physics_process(delta):
	move_camera()

Is the error happening because of the way I’m connecting the parent node? :thinking: I really have no idea :sweat_smile:

My bad. I’m used to C#, not so much to GDScript

You don’t need the parenthesis on global_position.

var target_pos = target.global_position
1 Like

All good!

So this does work! :

extends Camera2D

onready var target = $"../this_thing"

func move_camera():
	var mouse_pos = get_global_mouse_position()
	var target_pos = target.global_position
	offset = (mouse_pos + target_pos) / 2

func _physics_process(delta):
	move_camera()

Now, there does some to be an issue when clamped()/limit_length() is used. I get the issue with the camera not centering on the target.

I’m going to mess around with finding some numbers that might work to limit how much it moves. Will post an update if I find something. Let me know if you find something that works, too!

1 Like

Well, I did some toying around with numbers and found that this seemed to work to limit the camera movement to my liking:

extends Camera2D

onready var target = $"../this_thing"

func move_camera():
	var mouse_pos = get_global_mouse_position() / 2
	var target_pos = target.global_position * 1.5
	offset = (mouse_pos + target_pos) / 2

func _physics_process(delta):
	move_camera()

I am not sure if the math works out across all scenarios here as I am not sure how to check it and there may be unforeseen consequences to this code.

It would be wonderful if someone knows what’s going on with the numbers in this and could shed some light on them, lol.

So, slight problem with this code!

If the object the camera is focused on does not start on the world’s origin, the camera will be offset.

If anyone can think of why this may be happening and can think of a solution, it would be greatly appreciated!


I will update this thread with the updated code.

Figured it out!

The problem came from calling get_global_mouse_position()

Instead one should use get_local_mouse_position()

extends Camera2D

onready var target = $"../your_object"

func move_camera():
	var mouse_pos = get_local_mouse_position() / 2
	var target_pos = target.position * 1.5
	offset = (mouse_pos + target_pos) / 2

func _physics_process(delta):
	move_camera()

Extra fun stuff!

I am still not sure what is happening with the numbers in these lines:

	var mouse_pos = get_local_mouse_position() / 2
	var target_pos = target.position * 1.5

But there seems to be a ratio of some sort that needs to be met for the camera to work properly.

Here are the combos I have found to work so far. Maybe someone smarter than myself can figure out what’s going on.

	var mouse_pos = get_local_mouse_position()
	var target_pos = target.position
	offset = (mouse_pos + target_pos) / 2
	var mouse_pos = get_local_mouse_position() / 2
	var target_pos = target.position * 1.5
	offset = (mouse_pos + target_pos) / 2
	var mouse_pos = get_local_mouse_position() / 4
	var target_pos = target.position * 1.75
	offset = (mouse_pos + target_pos) / 2

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