0 votes

Hello guys I have made a simple space shooter but the issue is that I made it for windows. I move using the WASD keys and fire using space key. But now I want to transfer it to android and I'm not sure how to handle touch input.

func _physics_process(delta):
    var dirVec = Vector2(0,0)
    $EngineFlame.process_material.tangential_accel = 0.0
    if Input.is_action_pressed("move_left"):
        $EngineFlame.process_material.tangential_accel = -150.0
        dirVec.x = -1
    elif Input.is_action_pressed("move_right"):
        $EngineFlame.process_material.tangential_accel = +150.0
        dirVec.x = 1
    if Input.is_action_pressed("move_up"):
        dirVec.y = -1
    elif Input.is_action_pressed("move_down"):
        dirVec.y = 1

I want the ship to keep firing as long as the person is touching the screen and want the ship to follow the finger where ever the person takes it. How can I do that ?

in Engine by (403 points)

1 Answer

0 votes
Best answer

You can use InputEventScreenTouch to get the touch position. Because top left is the origin half of the screen size will have to be taken away to make the origin in the middle of the screen and then use normalized() so dirVec is a unit length of 1.

func _input(event):
    if event is InputEventScreenTouch:
        dirVec = (event.position - (get_viewport().size*0.5)).normalized()
by (3,221 points)
selected by

Thanks for reply I got confused with the above so I used this instead of my above code :

var motionx=(get_global_mouse_position().x-position.x)*0.2
var motiony=(get_global_mouse_position().y-position.y)*0.2

Now my player is following my mouse cursor. And I will assign the left mouse click to shoot action. Do you think now I can do something to make touch control easily ?

Edit : The game is working just fine on my phone and the player is moving just as I expected but he is not shooting. When I touch screen player follows my touch but I was expecting him to shoot also since I have assigned left click for shooting.

You can either use a TouchScreenButton or make it fire automatically on an InputEventScreenTouch event.

func _input(event: InputEvent) -> void:
    if event is InputEventScreenDrag and fireDelayTimer.is_stopped():

My player now fires when I keep my fingers moving but when I stop, no bullets get fired! I tried adding:

if event is InputEventScreenTouch and fireDelayTimer.is_stopped():

he still doesn't fire. But overall I am happy that at least he's firing now when I keep my fingers moving.

That's because your using InputEventScreenDrag which provides a starting point and a relative drag point. InputEventSceenTouch will be active until you stop touching the screen, but this event doesn't return a drag point.

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.