How to detect what object is tapped in mobile game?

Godot Version

4.2

Question

I am making a city builder. i got it to open a menu that shows what buildings can be made then you select a building and press a build button that spawns it( note additional question below) and closes the menu.
now i want to make it so if the player taps on the building it will allow it to show ui for info and if it taps and drags to move the building.

So do i need to make the building a button? or is a raycast a good way and how to use in mobile?

Additonal question:
when i spaw a building it spawns it under my ui node is there a way to spawn it in my world? where i have my tile map etc?

Learn about

im too stupid for the docs to make sense. so this didn’t really help my issue. but thanks tho

1 Like

Any idea why this wouldn’t work?

func _input(event):
if event is InputEventScreenTouch:
if get_rect().has_point(to_local(event.position)):
print(“You clicked on Sprite!”)

its in a script attached to a sprite 2d node but the sprite 2d is a child of a static body.
after experimenting more it seems the area to click is way far left of the sprite.

Read this page, did you changed the sprite scale or rotation?

Thanks! i did not change scale or rotation. after reading that link it didn’t help or i didn’t understand how to apply it to my situation.

Go to project settings > input devices > pointing or search it in project settings, and there, set the emulate touch from mouse to true

I had already done that. thanks tho. but you gave me a good idea to build to mobile and try it on my phone to see if it behaves differently.

1 Like