Hi, I'm learning Godot and was having a hard time to figure this out and couldn't find any tutorials that helped with my problem. So I am learning by trying to create a 2d top down shooter so right now I'm trying to get my players movement to move according to which way he is facing. I have the player looking at the mouse cursor but the player will always go up when I press 2. If anyone could help me that would be greatly appreciated :) my code so far is located below.


extends KinematicBody2D
var MAX_SPEED = 500
var ACCELERATION = 4000
var motion = Vector2.ZERO

func _physics_process(delta):
	var axis = get_axis()
	if axis == Vector2.ZERO:
		apply_friction(ACCELERATION * delta)
	else:
		apply_movement(axis * ACCELERATION * delta)
	motion = move_and_slide(motion)
	look_at(get_global_mouse_position())
	

func get_axis():
	var axis = Vector2.ZERO
	axis.x = int(Input.is_action_pressed("right")) - int(Input.is_action_pressed("left"))
	axis.y = int(Input.is_action_pressed("down")) - int(Input.is_action_pressed("up"))
	return axis.normalized()
	``
func apply_friction(amount):
	if motion.length() > amount:
		motion -= motion.normalized() * amount
	else:
		motion = Vector2.ZERO

func apply_movement(acceleration):
	motion += acceleration
	motion = motion.clamped(MAX_SPEED)

Thanks for replying I kind of understand from reading that doc just don't know how to apply it to my code

Well, your motion is applied after the lookat function. Your code is basing everything off the input. If you are using the mouse to steer, then you only need one key to press to tell it to move forward, or probably the left mouse button. Then you are applying acceleration and friction. Basically, I would use move and slide with a key and give it a fixed distance or speed to start while you are holding it down and off when it's up. Then apply acceleration and friction to the motion to make it look more realistic. So yeah, it's pretty much of a rewrite, but you can use it to help figure out the other parts. I don't really know what you are doing. Start simple, just put that code into a move and slide after a lookat and get input from a mouse click. Then get it to stop at the mouse position or before. Then add acceleration and friction. Unless you are trying to turn the ship to the direction of the motion. I don't know what you are trying to do. Maybe rotate the ship with the side keys and use the forward keys to go in the direction it's pointing? Then you wouldn't use lookat.

What I want is the player to move in the direction the player is facing rather than the direction of the mouse cursor because sometimes the mouse cursor will be on top of the player and would cause issues. but I want to still dictate where the placer is facing from the mouse. I also want strafing movement with the a and d keys. I hope that makes sense. Also thanks for your help so far!

I took the code from the 2d movement overview and it almost does what I want it to do, I still have the issue where if the mouse is on the player the player wont move, and it doesn't have strafing side to side.

export (int) var speed = 500

var velocity = Vector2()

func get_input():
	look_at(get_global_mouse_position())
	velocity = Vector2()
	if Input.is_action_pressed("down"):
		velocity = Vector2(-speed, 0).rotated(rotation)
	if Input.is_action_pressed("up"):
		velocity = Vector2(speed, 0).rotated(rotation)

func _physics_process(delta):
	get_input()
	velocity = move_and_slide(velocity)

If you want to strafe, and the player is pointing toward the x of his own transform, you would move him in the y direction + and - using the relative movement of the transform. If the mouse is on the player, the lookat function won't work.

I'm a little confused so in my current code I have Vector2(speed, 0).rotated(rotation) shouldn't it just be as simple as changing the y value to a positive 1 or a negative to move on the y axis? Obviously since it didn't work its not the way you do it but it just confused me

Where is the rotation variable coming from? That's probably what you would change to get it to move differently. You would want to move it at 90 degrees to the rotation in the rotated() function. Actually, I'm not sure how it's working though, because it is using the vector 2. I think that's mostly for speed, though. Not sure, really. Otherwise just use: transform.origin += transform.y * speed after the input

Thanks for the help! Finally got it to do what I want!

a year later