Search Unity

  1. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Question Creating a lock-on combat system in 2d

Discussion in 'Getting Started' started by lionel1234, Oct 13, 2023.

  1. lionel1234

    lionel1234

    Joined:
    Dec 19, 2020
    Posts:
    2
    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.
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,239
    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
     
  3. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    11,082
    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.
     
    Kurt-Dekker likes this.