This site is currently in read-only mode during migration to a new platform.
You cannot post questions, answers or comments, as they would be lost during the migration otherwise.
0 votes

Hi all,

I'm assuming something like this:

func _input(event):
    if event.type == InputEvent.SCREEN_TOUCH:
        # Do touchy touchy

I want two player though, so I'm guessing it would be something like:

func _input(event):
    if event.type == InputEvent.SCREEN_TOUCH:
        if touch is on L side of screen do left controller
        elif touch is on R side of screen  do right controller.

Before I commit to this I thought I'd ask and see what people think of this approach or if they can see any flaws / better solutions?

Thanks a tonne...

in Engine by (838 points)

I think I would do that too.

2 Answers

0 votes
Best answer

For future reference to help anyone else. This is how I'm doing it (so far working well):

I have a structure like so:

  • gameLevel

    • Area2DL (script here)

      • CollisionShape2D (rectangle shape)

    • Area2DR (script here)

      • CollisionShape2D (rectangle shape)

On the Area2DL and Area2DR there is a signal called input_event. I connect that signal to a custom function as shown in this complete script:

extends Area2D

var touchPos    =   Vector2(0,0)    #this will change when we touch the Area2D

func _ready():
    # Called every time the node is added to the scene.

func _fixed_process(delta):
    get_node("/root/gameLevel/batIndicatorL/KinematicBody2D").touchControl(delta, touchPos)

func _on_Area2DL_input_event( viewport, event, shape_idx ):
    touchPos = event.pos  #this is a Vector2 with local position of the touch

The get_node("/root/gameLevel/batIndicatorL/KinematicBody2D").touchControl(delta, touchPos) is essentially a function that drives the movement of my bat. It takes the delta and the touchPos and feeds it to the function like so:

func touchControl(delta, touchPos):
    if PlayerNumber == 1:
        move_to(Vector2(30, touchPos.y))
    elif PlayerNumber == 2:
        move_to(Vector2(510, touchPos.y))

In this way, the Area2D passes the location of the touch to the bat to move to that position in the Y only. Nice!
It's not perfect I'm sure but it's working so far. Now to test on a few devices.

Many thanks to volzhs and eons for the suggestions.

by (838 points)
+1 vote

If the game don't need drag, you can use big invisible TouchScreenButtons, then just control the input or events actions like any action, these nodes work fine with multitouch.

by (7,954 points)

That is interesting. I've not researched touch buttons. I'll take a peek and see how they behave

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 Frequently asked questions and 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 [email protected] with your username.