Area2d not stopping on touch position

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

I want to shoot a bullet at the position where finger was so I write

var touchPos = Vector2()
var speed = 10

in process:
var direction = touchPos - get_global_position().normalized()
position += direction * speed
if global_position.distance_to(touchPos) < 5
stop bullet

func _input(event):
 if event is InputEventScreenTouch:
    touchPos = event.get_position()

the condition in process is never true. If I write global_position.distance_to(touchPos) < 30 it’s true but then the bullet is not where the finger is exactly. it’s 29 pixels away.

I tried with if get_global_position == touchPos still not working. I checked in inspector the touchPos variable and position of area2d. position is always changing it’s value that’s why it doesn’t work I think. I don’t know how to fix this thing

Maybe the problem is this line?

var direction = touchPos - get_global_position().normalized()

The line should probably be written like this:

var direction = (touchPos - get_global_position()).normalized()

Ertain | 2022-01-31 01:01

:bust_in_silhouette: Reply From: rossunger

If the bullet’s speed is more than 10 pixels, then it might overshoot the touchPos, and then be >5 away, but in the opposite direction