Hi, I am making a simple top-down space shooter game.

Basically, I want it to play so that: When you click the left mouse button, you accelerate; When you let go, you keep moving in that direction, and; When you right click, you slow to a stop.

Right now I'm just working on the first thing. I've figured out how to make the ship point in the direction of the mouse. I currently have code that makes the ship go to where the mouse has clicked, but that's not quite what I want. Here's the relevant code:

extends KinematicBody2D

export var speed = 500

var target = Vector2()
var velocity = Vector2()

func _input(event):
	if event.is_action_pressed("forward"):
		target = get_global_mouse_position()
		
func _physics_process(delta):
	velocity = (target - position).normalized() * speed
	rotation = velocity.angle()
	if (target - position).length() > 5:
		move_and_slide(velocity)

"forward" here corresponds to a mouse click, and of course target=get_global_mouse_position() tells it to go to where the mouse has clicked. I'm just not sure how to tell it to consistently go where the mouse is pointing while you hold it down. Anybody know what I should try here?

you have to create a direction vector. Given A (your character global transform.origin) and B (mouse cursor position) the direction vector would be B-A.normalized() . then you can multiply it by the speed constant.

velocity = dirvec * speed
2 years later