How does one make a top-down 2d target-locking system?

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

Hello there, I’m attempting my first go at developing a game and it’s going to be a roguelike 2d bullet hell type game. I’ve seen many games like these already with a cursor system where the player shoots wherever the cursor is currently pointed. I was wondering if there was a way to remove the need for a cursor entirely and just have an auto targetting system that locks your aim at the nearest enemy visible in your line of sight.

Thank you in advance!

distance_to

if player.global_position.distance_to(enemy.global_position) < 10:
      print("bla bla")

10 = distance between player and enemy

ramazan | 2022-09-02 08:41

:bust_in_silhouette: Reply From: sarapan_malam

Hi, to get an enemy in front of the player we need to get an angle with a dot product

Maybe this sample code can help you :slight_smile:

-rotate without mouse
rotate_without_mouse
-rotate with mouse
rotate_with_mouse