Help with Screen Edge Pointer for Quest Marker in 2D Game (Godot 4.3)

Godot Version

4.3

Question

Hi everyone,

I’m working on a 2D game in Godot 4, and I’m trying to implement a pointer system for my quest marker. The pointer should behave as follows:

When the target (quest marker) is inside the camera’s visible area:

The pointer should be hidden.
When the target is outside the camera’s visible area:

The pointer should appear on the edge of the screen, pointing in the direction of the target.
The pointer does not need to rotate or change its appearance based on direction (it’s just a static circle).

Here’s my current setup:

Main is the root node.
The player is a CharacterBody2D node with a Camera2D child that follows the player.
The Pointer is a Node2D with a Sprite2D child for the visual representation.
The quest marker is a Node2D placed somewhere in the world.
I’ve tried implementing a solution based on screen edge clamping, but the pointer either doesn’t appear or behaves incorrectly. Here’s what I have so far:

extends Node2D

@export var target: Node2D = null  # The quest marker
@export var camera: Camera2D = null  # The player’s camera

var screen_size: Vector2

func _ready():
    screen_size = get_viewport().size

func _process(delta):
    if not target or not camera:
        hide()
        return

    var target_pos = target.global_position
    var camera_pos = camera.global_position
    var offset = target_pos - camera_pos
    var half_screen = screen_size * 0.5

    if abs(offset.x) <= half_screen.x and abs(offset.y) <= half_screen.y:
        hide()
    else:
        show()
        var direction = offset.normalized()
        var edge_pos = direction * half_screen
        edge_pos.x = clamp(edge_pos.x, -half_screen.x, half_screen.x)
        edge_pos.y = clamp(edge_pos.y, -half_screen.y, half_screen.y)
        position = edge_pos + half_screen

However, the pointer doesn’t appear correctly when the target is off-screen, and sometimes it’s hidden even when it should be visible.

Could someone help me troubleshoot this or suggest a better approach? I also came across this tutorial: Figuring Out Godot 2D Off-Screen Pointers and Bonus Picture-In-Picture – Input Randomness , but I’m struggling to adapt it to my game.

Any advice or examples would be greatly appreciated. Thanks!

Hi @nikolacvijanov

Maybe that I’m wrong, but I think that your ‘half_screen’ is the red rectangle in the image, and you desire the green rectangle. For this you have to add an offset to X and Y in the IF statement.
image

Possible that the ‘offset’ also needs to be an ‘absolute’ position on the screen instead of the ‘relative’ position that you created with the abs() function. As a third possible issue I expect the clamping function.

I would suggest to add a debug window showing the position values for target/camera/offset and half_screen. This can help with why the IF statement is not performing as expected.

Split the IF issue in 2 parts. Start e.g. with only the X position checking until you have the desired behavior. Then you can focus on the Y position.

With kind regards

Ludo