Axe projectile offscreen indicator not correctly tracking axe's position

Godot Version

v4.3.stable.official [77dcf97d8]

Question

I’m trying to have an offscreen indicator that shows the player where to go find the axe they threw. But I’m having trouble with it. The offscreen indicator either appears in the incorrect spot and doesn’t move or simply disappears with the axe.
Can anyone help me figure out what I’m doing wrong or link me to an answer for reference? I’ve looked around but couldn’t find anything.

Here’s my code:

extends Node2D

@export var tracked_object: VisibleOnScreenNotifier2D # Currently unused, but feel free to utilize it if needed
@onready var sprite = $TextureProgressBar

# Called every frame. 'delta' is the elapsed time since the previous frame.
func _process(delta):
    var canvas = get_canvas_transform()
    var top_left = -canvas.origin / canvas.get_scale()
    var size = get_viewport_rect().size / canvas.get_scale()
    
    set_marker_position(Rect2(top_left, size))

func set_marker_position(bounds: Rect2):
    sprite.global_position.x = clamp(global_position.x, bounds.position.x, bounds.end.x)
    sprite.global_position.x = clamp(global_position.y, bounds.position.y, bounds.end.y)
    
    if bounds.has_point(global_position):
        hide()
    else:
        show()
        print(global_position)

Here are screenshots of my scene docks:
godot help - axe scene tree

godot help - axe offscreen indicator scene tree

Here’s a video showcasing my issue (this is a link).

Unfortunately I didn’t have enough time to solve the problem myself (I was making this game for a game jam) so my solution was to just use a premade asset lol

You could try to solve this with linear algebra:
take the position of your offscreen-object, scale it down, scale it up to border of screen.

p.e:
var ratio = offscreen_element.position.y / bounds.position.y
marker.position.x = offscreen_element.position.x * ratio
marker.position.y = offscreen_element.position.y * ratio

1 Like

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