Attention | Topic was automatically imported from the old Question2Answer platform. | |
Asked By | Goobis |
I’m trying to get some basic ship movement down for a game i’m working on and i’m running into an issue. The ship will aim at the mouse just fine and it will accelerate and slow down with friction but it won’t accelerate towards the mouse. Thanks for any help. Here’s the code I have so far.
extends KinematicBody2D
export (int) var speed = 500
export (float) var rotation_speed = 1.5
export (float) var friction = 2
var velocity = Vector2()
var rotation_dir = get_global_mouse_position()
var acc = Vector2()
func _physics_process(delta):
#Aiming the ship
_look_at_mouse()
_movement(delta)
func _look_at_mouse():
var vert = get_global_mouse_position()
var gpos = self.get_global_position()
if gpos.x > vert.x:
$Sprite.set_flip_v(true)
vert.x = vert.x
else:
$Sprite.set_flip_v(false)
look_at(vert)
func _movement(delta):
if Input.is_mouse_button_pressed(BUTTON_RIGHT):
acc = Vector2(0, -speed).rotated(rotation)
else:
acc = Vector2(0, 0)
velocity += acc * delta
move_and_slide(velocity, Vector2(0, 0))
velocity = velocity.linear_interpolate(Vector2(0, 0), friction * delta)