Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice

Question How do I activate an inactive enemy when I go near her?

Discussion in 'Scripting' started by energiva21, Feb 7, 2024.

  1. energiva21

    energiva21

    Joined:
    Mar 4, 2023
    Posts:
    64
    I'm making an RPG game and there are a lot of enemies in the game and for optimization I want them to pop up when I go near them. However, since the object is not active, I can neither measure its distance with my character nor detect and open it with collider. What advice would you give me for this problem?
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,656
    Make sure you actually need this optimization or else you're just making a complicated mess of your program.

    I would just leave them all on all the time.

    Otherwise, leave some script on somewhere that watches the player and wakes up a particular enemy.

    Alternately, make enemies have a sub-portion that can be turned off to make them go away, and a base portion that stays on:

    EnemyBaseWithWatchForPlayerScript
    EverythingElseInTheEnemyThatGetsTurnedOff
     
  3. kdgalla

    kdgalla

    Joined:
    Mar 15, 2013
    Posts:
    4,626
    I tend to make a "spawn point" object for each enemy or a group of enemies. It's an invisible object with no mesh. but handles when to create enemy objects based on a OnTriggerEnter or distance check.

    The advantage making a separate object from the actual enemy is that you can also use this object to handle re-spawn logic. For example, after all of the original enemies have been destroyed then maybe it won't spawn new enemies again until a certain amount of time has passed or something like that.
     
    Kurt-Dekker likes this.
  4. energiva21

    energiva21

    Joined:
    Mar 4, 2023
    Posts:
    64

    I'm making an RPG game and there are more than 2000 enemies in the game. When all of them are unlocked, the game gets stuck a lot. The game works better with the second method you suggested.
     
  5. energiva21

    energiva21

    Joined:
    Mar 4, 2023
    Posts:
    64

    Thank you, I am trying the method you mentioned now. I will write when I finish.
     
  6. AngryProgrammer

    AngryProgrammer

    Joined:
    Jun 4, 2019
    Posts:
    490
    In alternative version you can just make invisible trigger object around own character (don't has to be around each enemy). So if trigger bump enemy you tell them to activate. Trigger for each enemy will be better if you plan to have different trigger range.
     
  7. energiva21

    energiva21

    Joined:
    Mar 4, 2023
    Posts:
    64
    I divided the enemies into groups of 10 and 15, and managed their opening and closing through parenting. When I do this individually, while measuring the distance, the computer was running slowly. It worked better in group form.