0 votes

Hey,

I am working on a small prototype that works with tilemaps. At a certain point, the player needs to drag and drop a button on the tilemap. The code is working fine on PC but not on mobile devices, where the correct position is always offset a little.

The button that will be dragged:

func get_drag_data(_position):
    var preview = button.duplicate()
    set_drag_preview(preview)
    return self

The UI above the tilemap that will receive the drop:

func drop_data(position, _data):
    EventHub.emit_signal("skill_dropped",position)

As I've said, the above code works on PC, but not on mobile devices, as I always need to touch a tad above the "correct" spot.

I've tried to get the global_position, but nothing has changed:

func drop_data(position, _data):
    var global_touch_position = get_canvas_transform().xform_inv(position)
    print(global_touch_position)
    EventHub.emit_signal("skill_dropped",global_touch_position)

Edit: With more testing, I've found that the problem lies when the screen is stretched or loses the original aspect ratio, as can be seen in the images below:

Here the code works as intended and the touched position corresponds exactly with the selected tile:
Everything works fine

But here I need to touch a bit higher than the right position to select the desired tile:
Now the touch is not precise

I need to get the touch position taking into account these screen stretches/aspects.
Any suggestions?

Thanks,
Lucas Sene

Godot version 3.2.3
in Engine by (42 points)
edited by

Please log in or register to answer this question.

Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to webmaster@godotengine.org with your username.