Get a ball to go towards mouse clicked position

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

Hi guys!

First time ever coding a game (or coding in general), and I’ve been learning lots. I’ve been trying to get a ball to move towards the coordinates where my mouse has clicked. I don’t really quite understand all the vector things yet, so I’m having trouble with what’s happening here.

func _input(event):
if event.is_pressed(): # Mouse click coordinates
	velocity.x = event.position.x - global_position.x
	velocity.y = event.position.y - global_position.y

So far, the event.position gets the coordinates where my mouse presses. Yay!
The ball goes in the right direction, but at an insane speed. Any tips? Thank you!

:bust_in_silhouette: Reply From: akoutsoulelos

Look at this tutorial:

Godot 2d movement tutorial

Generally, velocity is the distance added to your ball. You calculate the distance between the cursor and the ball and then you add it to the ball position, that’s why the ball runs so fast - you make it move directly to the cursor position.
You should normalize the distance to find the direction, then multiply it by the wanted speed and by the delta time passed.