Bullet is moving wrong

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

So when I click on the screen I want the bullet to go in this position. It’s working but when it reaches the end point its moving to left and right

this is the position of touch X538 Y181
the bullet is changing from X542 to X521
area2d
var touchPos = Vector2()
var speed = 22
func _process(delta):
var direction = (touchPos - get_global_position())
position += direction.normalized() * speed

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

I guess it’s because of normalized. So how can I fix it?

:bust_in_silhouette: Reply From: Inces

No. It is because You didn’t code end of movement. Your touch target position remains there when bullet reaches it, so it will jitter around it, constantly adding and subtracting direction towards it. You need to add a line

 if global_position.distance_to(touchpos) < some chosen range :

then stop. You can add this line as an inverted condition before movement code ( only move like this when you are not close to target ) or You can add it to the top of physics process, followed by return ( stop any activity whenever you are close to target )