I need to get global event.position

Godot Version 4.3

Hello I’m new at Godot and I need help

I need to get global event.position and get coordinates like get_global_mouse_position() give to use look_at()

this code with event.position its the same like → get_viewport().get_mouse_position()

but I need to get with event.position the same coordinates like return get_global_mouse_position()

Code have here:

extends Sprite2D
			
func _input(event):
	if event is InputEventScreenTouch and !event.is_pressed():
		look_at(event.position)

please help me)

Do you use a camera2d?
If yes then you can add the global_position of the camera to the event.position to get the global_position of the event.

1 Like

Thank you for your response. Yes, I use Camera2D, but when I trying to do this I get the same result

extends Sprite2D
			
@onready var camera : Camera2D = $"../Camera2D"

func _input(event):
	event.position = camera.global_position
	if event is InputEventScreenTouch and !event.is_pressed():
		look_at(event.position)

Do you mean smth like this?

you have to add the global_position:

look_at(event.position + camera.global_position)

It could be that there is an offset of half the screen-size/width. In this case just subtract this constant amount

no, unfortunately it doesn’t work how I want(

func _input(event):
	if event is InputEventScreenTouch and !event.is_pressed():
		print("what i need to change: ", event.position)
		print("result: ", event.position + camera.global_position)
		print("what i need to get: ", get_global_mouse_position())

output:

what i need to change: (1693, 1355)
result: (1843, 1421)
what i need to get: (141, 209.5)

Subtract the result by a Vector of half the screen-width and height to get the correct position

1 Like

Super! It’s almost that I need. But I got result a little different. Thank you for your help!
If you have some thoughts how I can do it?

func _input(event):
	if event is InputEventScreenTouch and !event.is_pressed():
		print("what i need to change: ", event.position)
		print("result: ", event.position-get_viewport_rect().size/2)
		print("what i need to get: ", get_global_mouse_position())

Output:

what i need to change: (1718, 1306)
result: (8, 237)
what i need to get: (154, 185.5)

you still need to add the camera-global_position

1 Like

Thank you a lot. Finally I got it :handshake:

1 Like

And maybe you know how can I detect position if I use horizontal enable and vertical enable option activated?

i have no idea. Why dont you use get_global_mouse_position()?

Because I’m using a joystick and when I click on the screen get_global_mouse_position() defines the first click as the position

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