Rotation change based on mouse position

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

Hello! I have been working on this little project for sometime. I’m very slow on it. I am having problems with movement.

    extends KinematicBody2D

onready var sprite = $Sprite

export var max_speed := 500
var direction := Vector2.ZERO

export var drag_factor := 0.5
var desired_velocity := Vector2.ZERO
var steering_vector := Vector2.ZERO
var velocity := Vector2.ZERO


func get_input(delta) -> void:
	look_at(get_global_mouse_position())
	
	direction = Vector2(Input.is_action_pressed("move_up"), Input.is_action_pressed("move_right")).rotated(rotation) - Vector2(Input.is_action_pressed("move_down"), Input.is_action_pressed("move_left")).rotated(rotation)
	
	desired_velocity = direction * max_speed
	steering_vector = desired_velocity - velocity
	velocity += steering_vector * drag_factor * delta
	velocity = move_and_slide(velocity)
	



func _physics_process(delta: float) -> void:
	get_input(delta)

My problem I am having is two things.
I can’t seem to get the rotation to match the direction. So if I press left key I always want it to go left. the problem is that is always rotate clockwise and will not rotate counter clockwise when the mouse is below it. I tried other methods but it didn’t work.

The last thing is when my KinematicBody2D gets right up to the mouse it just keeps rotating and moving and I am not sure how to fix that.

I’m not asking for a straight answer I’m really lookin for someone to point me to the right direction on how to fix it. but if my code is too rigid please let me know how to fix that.

:bust_in_silhouette: Reply From: Wakatta

I’m really lookin for someone to point me to the right direction

Lol saw what you did there :slight_smile:

The right direction

if your goal is a Hotline Miami style then you’re on the right path code wise and for mouse rotations(anti / clockwise) you can always do a distance check. Going to stop typing now since you did not want a straight answer.