Creating a lock-on combat system in 2d

I’ve been working on a combat system for my top down game that involves precise inputs, and I wanted to use a lock-on system similar to this:
https://www.reddit.com/r/IndieDev/comments/ycttvt/created_a_lock_on_system_for_my_top_down_2d/

How exactly is something like that implemented? Would I have to calculate the angle between player and enemy?

Any tutorial I find is about using the mouse to change the player’s direction towards enemies, which is not exactly what I want.

You won’t find “exactly” what you want.

Cartesian (X/Y) to angular conversions are the same.

  • use Mathf.Atan2() on the positional difference
  • remember that Atan2() outputs in radians
  • radians can be converted to degrees by multiplying by Mathf.Rad2Deg

Tutorials are not free code, they’re there to teach you things. If you have a tutorial that’s showing you an angle in reference to a mouse position, how is it not teaching you to calculate the angle? Surely it doesn’t matter what a specific position is being set by be it the mouse, a field in a script, some Transform position or anything else!

FYI: I’ll remove the 2D physics tag and this is nothing to do with physics.

1 Like