Anyone knows how to make "get_viewport().get_mouse_position()" position 0,0 to be in the middle?

anyone knows how to make “get_viewport().get_mouse_position()” position 0,0 to be in the middle?
i need it to be in the middle of the screen instead of the top left

heres my code:

extends TileMap

func _ready():
	Input.set_mouse_mode(Input.MOUSE_MODE_CONFINED)

func _process(delta):
	if Input.is_action_just_pressed("click"):
		var mouse_pos = get_viewport().get_mouse_position()
		erase_cell(0, mouse_pos)
		print(mouse_pos)

can you work with this:

var mouse_pos = get_viewport().get_mouse_position() - (get_viewport.size() / 2)
2 Likes

its not working but thanks

what do you mean by “not working”? Whats happening?

1 Like

I think it’s

get_viewport().get_visible_rect().size / 2

Capture

1 Like

So it’s a compilation error, what is the stated error with that line?

Obviously get_viewport is a method and not a variable :sweat_smile:. This should work though: EDIT

var mouse_pos = get_viewport().get_mouse_position() - (get_viewport().size / 2)

If the viewport is a window then size is a variable not a function :upside_down_face:

get_viewport().size / 2
1 Like

I clearly looked at too much code for today. Thanks for the fix :sweat_smile:

1 Like

its saying it cant make a minus with vector 2i and vector 2

Then convert it to Vector2:

var mouse_pos = get_viewport().get_mouse_position() - Vector2(get_viewport().size / 2)

thanks

1 Like

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