Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice
  4. Dismiss Notice

Question Cycle between enemies when locked on

Discussion in 'Scripting' started by s0lidsnake, Jul 14, 2021.

  1. s0lidsnake

    s0lidsnake

    Joined:
    Oct 8, 2019
    Posts:
    8
    Hello. So ...I was working on a lock-on system for my top-down shooter and everything was fine, until I realized, that I have no idea how to cycle between enemies in an array. Basically, the player turns toward another enemy if they are closer than the previous one, but I want to be able to cycle between enemies that are at the same distance. Thanks for your help in advance!

    P.S Here is the code so far.

    Code (CSharp):
    1. void CheckDistToEnemy()
    2.     {
    3.         enemies = GameObject.FindGameObjectsWithTag("Enemy");
    4.  
    5.         for (int i = 0; i < enemies.Length; i++)
    6.         {
    7.             if (Vector3.Distance(this.transform.position, enemies[i].transform.position) <= distToAimAtEnemy)
    8.             {
    9.                 if (Physics.Raycast(this.transform.position, enemies[i].transform.position))
    10.                 {
    11.                     enemies[i].gameObject.GetComponent<Renderer>().material.color = Color.red;
    12.                     Vector3 targetPos = new Vector3(enemies[i].transform.position.x, transform.position.y, enemies[i].transform.position.z);
    13.                     player.LookAt(targetPos);
    14.                 }
    15.             }
    16.             else
    17.             {
    18.                 enemies[i].gameObject.GetComponent<Renderer>().material.color = Color.white;
    19.             }
    20.         }
    21.     }
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,779
    Due to floating point imprecision, two enemies will almost never be "the same" distance, so instead think of it as "similar distances"

    However, your proposed approach will cause a problem for the user because if two enemies are "sorta" close in distance, the user CAN select between them, but if a third enemy is just a little farther, the user can't select him for some mysterious reason, and depending on orientation, it might be very frustrating.

    You might want to instead just implement a basic "allow lockon and switching of all enemies within X range," which is basically what Souls-like games do.

    Fortunately this is very well-travelled ground:

    https://www.reddit.com/r/Unity3D/comments/abprw5/lock_on_system_similar_to_dark_souls/

    And the requisite 46,600 tutorial videos:

    Screen Shot 2021-07-14 at 10.33.27 AM.png
     
  3. s0lidsnake

    s0lidsnake

    Joined:
    Oct 8, 2019
    Posts:
    8
    I might switch to maybe a trigger zone instead of float in the future, because yeah, I thought about those problems.
    And regarding vids on YT, I think they all use a camera-based lock-on ( which is not what I need, as far as I know). But I haven't checked every single one tho. Still, thanks for your reply.
     
    MartinMa_ likes this.
  4. MartinMa_

    MartinMa_

    Joined:
    Jan 3, 2021
    Posts:
    455

    Good for you , invent something new then copy paste google.However in this video are some really interesting points about camera
    https://unitycodemonkey.com/video.php?v=N3eptfPnbec
     
    Last edited: Jul 14, 2021