how to make player move with touch?

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By Jr_Dan

hi all, i’m new to godot engine, and i’m trying to make an easy bullet hell for android.

i’m using
if InputEventScreenTouch == true:

and then i don’t know how to set the player’s position to the (x,y) coordinates just pressed. i thought i could just write

position = InputEventScreenTouch.position

or something similar…

how to access the vector2 generated by a screen touch event?

:bust_in_silhouette: Reply From: kidscancode

First of all InputEventScreenTouch is an event object, not a boolean. You would want to test

func _input(event):
    if event is InputEventScreenTouch:

And then you can test for pressed or released and set the player’s position like so:

func _input(event):
    if event is InputEventScreenTouch and event.pressed:
        player.global_position = event.position

This would teleport the player to the touch location. If you would rather move there, you can set a target and then move the player towards that target. See
for a full example of this style of character movement.

thanks dude, i still have to learn the engine. i’ll try

Jr_Dan | 2019-03-17 17:17

How do i swipe left and make my character move left at a constant velocity and same in other directions.

I want to make a snake game that uses swipe to move

Mathuthuka | 2019-03-18 03:34

For that you can use InputEventScreenDrag, which has a relative property indicating the direction of the swipe.

func _input(event):
    if event is InputEventScreenDrag:
        if event.relative.x > 0:
            # move right
        if event.relative.x < 0:
            # move left
        # etc

kidscancode | 2019-03-18 06:27

Its not working

I did this

extends KinematicBody2D

var speed = 200
var velocity = Vector2 ()

func _input(event) :
Velocity = Vector2 ()
If event is InputEventScreenDrag :
If event.relative.x > 0 :
velocity.x += 1
print (‘right’)

      velocity = velocity.normalized ()

The print works when i drag but the kinematicbody2D doesnt move.

How do i move ?

Mathuthuka | 2019-03-18 15:35

Your code is not formatted correctly, please do that so that it is readable.

I don’t see any movement code there. You’re setting the velocity, but without then using the velocity with move_and_slide() or move_and_collide() a KinematicBody2D doesn’t move.

Please see Using KinematicBody2D if you’re unfamiliar with how it works.

kidscancode | 2019-03-18 15:44

Sorry I’m so late to this post, but I followed what you (kidscancode) said, and I’m wondering if there’s a way to have the player continue moving in that direction instead of having to continuously drag again and again.

doinsdf | 2020-10-13 22:56

You should probably open a new question rather than commenting on old ones.

As for your question, just don’t set the velocity to 0 when the screen touch ends.

kidscancode | 2020-10-13 23:09