Issue with conflicting global_positions

Godot Version

Godot 4.2

Question

I’ve been trying to make a simple throw mechanic for a top-down 2D game, where a held item is thrown towards the mouse cursor. As I understand it, this should be done with something like

var velocity_vector = (get_global_mouse_position() - global_position).normalized()
apply_central_impulse(velocity_vector * modifier)

However, the global_position of the held item is always the same when I press throw, so it always goes in the wrong direction.

I checked this with print statements -

func _process(delta):
	print(global_position)
func throw():
        print("global_position: " + var_to_str(global_position))

which shows the discrepancy between the global_position values -
image

The value printed by _process() updates & changes every frame correctly, but no matter how many times I press throw when I move, the global_position is always (92,1). I want to access the global_position being printed by _process() to calculate the throw, does anyone know how to do that?

I think this is due to the difference in Viewport vs CanvasLayer coords after I found Viewport and canvas transforms — Godot Engine (stable) documentation in English, but after trying a ton of different conversions I still can’t seem to find the right global_position I want :cry: If anyone has more info on how I can do this, I would really appreciate it

Can you show the full script? Do you have any warnings?

Well… this is embarrassing. Just dug through my code, wrote up a whole explanation, and found two lines of code out of order that was changing the base position of the item the frame as I threw it :face_with_head_bandage:

Thanks for forcing me to finally debug this properly I guess, I’ll close this as resolved.

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