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
- CollisionShape2D (rectangle shape)
- 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:
#Script: areaTouchL.gd
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.
set_process_input(true)
set_fixed_process(true)
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.