How do I detect when my player has reached the cursor and have it stop constantly flipping rotations?

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

I am trying to make a sort of top down bullet hell type game. The user controls the player by using the mouse. After pressing enter, the sprite will continue to follow the mouse.
The problem I’m facing is that once it reaches the mouse, the sprite starts flipping its rotation constantly and it bugs me.

extends "res://MOB.gd"

 var has_clicked = false
 func control(delta):
     look_at(get_global_mouse_position())
     velocity = Vector2()
     if Input.is_action_pressed("ui_accept"):
	  has_clicked = true
         if has_clicked == true:
	  $AnimatedSprite.play("movement")
	    velocity = Vector2(speed, 0).rotated(rotation)

This is the code it extends from.

extends KinematicBody2D

signal health_change
signal dead

export (PackedScene) var Bullet
export (int) var speed
export (float) var cooldown
export (int) var health
export (float) var rotation_speed 

var velocity = Vector2()
var can_shoot = true
var alive = true

func _ready():
   $Timer.wait_time = cooldown

func control(delta):
    pass

func _physics_process(delta):
    if not alive:
	   return
    control(delta)
    move_and_slide(velocity)
:bust_in_silhouette: Reply From: kidscancode

Just disable movement if the distance to the target is below a small amount:

if position.disctance_to(get_global_mouse_position()) > 5:
    velocity = Vector2(speed, 0).rotated(rotation)

FYI, there’s not much need to check if has_clicked is true on the line after you assign it to be true. Or is it just that your indentation is messed up?

The solution sorted it out, thanks. Also I think it was just the indentation that was messed up.

Mi1k | 2020-03-30 03:16

:bust_in_silhouette: Reply From: lucaslcode

you could put an if statement before look_at so it only looks at the mouse if its more than 2 pixels away.

Thanks, the solution was awfully simple. I’m still not used to all the functions godot has.

Mi1k | 2020-03-30 03:15