Skip to content Skip to sidebar Skip to footer

Holding Screen Touch In Godot

I'm not sure I'm doing things the right way, but I started testing a draft of my 3D game on PC, then I exported it on android. First, I wanted my player to follow my mouse pointer

Solution 1:

First of all, know that you can emulate mouse input from touch, you will find the option in Project Settings -> Input Devices -> Pointing. It won't do multi-touch, but that is usually the easier way to do it. In particular if you want to keep your _physics_process code.


Anyway, let us talk about handling touch input. You need to be aware of two events: InputEventScreenTouch and InputEventScreenDrag. You are missing the second one. And of course to keep mouse support InputEventMouse (which has two kinds InputEventMouseButton and InputEventMouseMotion).

In fact, I would do this in my code:

if !(eventis InputEventScreenDrag)\
and !(eventis InputEventScreenTouch)\
and !(eventis InputEventMouse):
    return

For multi-touch support, you can distinguish each touch by its index. Except mouse does not have an index. To generalize, we can consider mouse to be index 0, which lead us to this:

var touch_index = 0;
if !(eventis InputEventMouse):
    touch_index = event.get_index()

To know if the user is dragging, you can check like this:

var is_drag = eventis InputEventScreenDrag oreventis InputEventMouseMotion

If it is not a drag, it is either a press or a release event. You can distinguish those two by checking event.is_pressed(). I need to reiterate, event.is_pressed() is only relevant when is_drag is false. Let me put it this way:

var is_drag = eventis InputEventScreenDrag oreventis InputEventMouseMotion
var is_press = !is_drag andevent.is_pressed()
var is_release = !is_drag and !event.is_pressed()

By the way, for the mouse, you can use button_mask to figure out which buttons are pressed. For example:

var is_left_click = eventis InputEventMouse\
                    and ((eventas InputEventMouse).button_mask & BUTTON_LEFT) != 0

You may want to add that logic to is_press if you only want left click to count, for example.

And finally for the position event.position works, even for mouse. And if you want the position in the local coordinates of some CanvasItem (Node2D or Control), you can use make_input_local.

Post a Comment for "Holding Screen Touch In Godot"