|
|
|
 |
Reply From: |
johnygames |
I think I found a way to do this. Now, I am no mathematician, but I figured that what you want is to calculate the normalized distance between two vectors and move the player using that result.
So, here’s how I implemented it:
First, I got the normalized distance between the start position and end position.
PP = (p1 - p2).normalized()# p1 is a Vector2 holding the start position, p2 is the end position
Then I updated the position of the player.
player.position = player.position-(PP*3)
Notice how I subtracted PP from the player’s current position instead of adding it.
This should work as a basis for swiping events. You could also do more advanced stuff, like calculating the angle between the swipe and the x axis and then use that to make your player face the same direction. You can read all about that here: Vector math — Godot Engine (3.1) documentation in English
Finally, there is a video I found which explains a bit more about swiping events: https://www.youtube.com/watch?v=7XlMqjikI9A
Awesome.It works like a charm, the player moves the right way! I was stucked but now I can move along.
Thanks a lot for the links to learn more too.
Danicano | 2019-08-08 16:37
Hi, I am a beginner and I wanted to try to do something like this and after a whole day of research, I couldn’t even get the detecting swipes part right. So I was wondering if you could share how did you do it.
I’m aware it might be too much to ask and don’t expect much but man I would really appreciate the help.
mauu21 | 2019-10-27 22:50
First you need a controller node, one where you can place the swipe logic. Once you have that, you can write the following in the Node’s script:
extends Node2D
var velocity = Vector2(100,-100)
var PP = Vector2(0,0)
var s1
var s2
onready var p1 = get_node('/root/Node2D/Player') # object that acts as a guide for swiping
onready var p3 = get_node('/root/Node2D/Player3') #object affected by the swipe
# Called when the node enters the scene tree for the first time.
func _ready():
pass
# Called every frame. 'delta' is the elapsed time since the previous frame.
func _input(event):
if event.is_action_pressed("mouse_click"):
s1 = get_global_mouse_position() # get initial position
if event.is_action_released("mouse_click"):
s2 = get_global_mouse_position() # get final position on release of mouse button
PP = (s1 - s2).normalized() # obtain the movement vector
func _process(delta):
#print(PP)
p3.position = p3.position-(PP*16) # move player by mutiplying the vector we got by a factor of your choice (I chose a speed of 16)
johnygames | 2019-10-29 03:54