I'm a tad confused on how you are wanting to move the player. If possible, can you link to a video and/or picture and/or game that shows the type of movement you are wanting to achieve? I am just having a hard time wrapping my head around the issue.
To move the character like in the game Enter the Gungeon, for example, you want to not use the rotation/direction of the player, and instead move the player relative to the origin of the world.
The following code should move the character relative to the origin of the world, like the movement seen in games like Enter the Gungeon:
var dir = ((get_global_mouse_position() - position).normalized())
var speed = 500;
look_at (get_global_mouse_position())
if (Input.is_action_pressed("ui_right")):
move_and_slide(Vector2.RIGHT * speed)
if (Input.is_action_pressed("ui_left")):
move_and_slide(Vector2.LEFT * speed)
if (Input.is_action_pressed("ui_up")):
move_and_slide(Vector2.UP * speed)
if (Input.is_action_pressed("ui_down")):
move_and_slide(Vector2.DOWN * speed)
if (Input.is_action_pressed("click")):
move_and_slide(dir * speed)
If you want to move the player relative to it's rotation but you do not want the circular looking movement, then you need store the reference direction before moving so that as the player moves, it is not effected by the change in rotation.
The reason the circular movement is occurring is because look_at is being called every frame, which changes the rotation of the sprite as it moves. Same with direction, as the player moves the vector pointing to the mouse changes, changing the angle to the mouse position.
There are two relatively easy ways to work around this. The first is not calling look_at every frame and using the rotation to get the left/right vectors:
var dir = ((get_global_mouse_position() - position).normalized())
var speed = 500;
if (Input.is_action_pressed("ui_right")):
var right_direction = Vector2(cos(rotation + PI/2), sin(rotation + PI/2));
move_and_collide(right_direction * speed * delta)
if (Input.is_action_pressed("ui_left")):
var left_direction = Vector2(cos(rotation - PI/2), sin(rotation - PI/2));
move_and_collide(left_direction * speed * delta)
if (Input.is_action_pressed("ui_up")):
var forward_direction = Vector2(cos(rotation), sin(rotation));
move_and_slide(forward_direction * speed)
if (Input.is_action_pressed("ui_down")):
var forward_direction = Vector2(cos(rotation), sin(rotation));
move_and_slide(-forward_direction * speed)
if (Input.is_action_pressed("click")):
look_at (get_global_mouse_position())
move_and_slide(dir * speed)
The second way is to store the rotation direction in a class variable, and update that class variable when the player clicks the mouse:
var movement_direction = Vector2.ZERO;
func _physics_process(delta):
var dir = ((get_global_mouse_position() - position).normalized())
var speed = 500;
look_at (get_global_mouse_position())
if (Input.is_action_pressed("ui_right")):
var right_direction = movement_direction.rotated(PI/2)
move_and_collide(right_direction * speed * delta)
if (Input.is_action_pressed("ui_left")):
var left_direction = movement_direction.rotated(-PI/2)
move_and_collide(left_direction * speed * delta)
if (Input.is_action_pressed("ui_up")):
move_and_slide(movement_direction * speed)
if (Input.is_action_pressed("ui_down")):
move_and_slide(-movement_direction * speed)
if (Input.is_action_pressed("click")):
movement_direction = dir;
move_and_slide(dir * speed)
Hopefully one of those code snippets helps!