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

Most efficient way of activating enemies

Discussion in 'Scripting' started by serbusfish, Jan 26, 2017.

  1. serbusfish

    serbusfish

    Joined:
    Dec 27, 2016
    Posts:
    247
    My current project is a side scroller, the backgrounds move and the players view area is static. With this in mind what would be the best way to activate enemies? So far I simply have the enemies off screen and use a waypoint system to delay when they will move into the viewing area. This is ok to use with some enemies, but when a level is fully stocked i'm concerned having hundreds of enemies waiting off screen could affect performance.

    I know there is a script command that can enable or disable game objects, but how would I go about trigger this command? Its not like I can set up area for the player to collide with as he moves along as it is the background that moves not the player.
     
  2. Aldo

    Aldo

    Joined:
    Aug 10, 2012
    Posts:
    173
    Object pooling will help you with the enemies

    For the "moment to create them" you Han have area triggers "close" enough to instantiate the enemies before they are inside camera, or look for "on camera enter" and keep a spawn point with the script
     
  3. Mordus

    Mordus

    Joined:
    Jun 18, 2015
    Posts:
    174
    I don't really do sidescrollers and games with 'enemies' so there may be a more conventional way of doing this, but my first thought is that I would make an 'enemyspawner' component. Give it a reference to the prefab of the enemy that needs to be spawned and slap it on an empty game object positioned at the location where it should spawn that enemy. I would then make an area trigger large enough to cover the players viewable area + a margin around it. When a enemyspawner enters the trigger area it spawns the provided enemy reference at its location. Your background moving while the player staying static shouldn't make any different, a trigger area will work the same regardless of whether its the trigger area that moves over the object or its the object that moves into the area.
     
  4. serbusfish

    serbusfish

    Joined:
    Dec 27, 2016
    Posts:
    247
    OK thanks guys, i've had a little mess with a trigger area script which ive attached to the player but it isnt working properly. It IS spawning the enemy in the correct location but its happening instantly so obviously the trigger area isnt being used by the script. I have probably overlooked something obvious (im new to Unity).

    This is what I have:

    Code (csharp):
    1.  
    2. using UnityEngine;
    3. using System.Collections;
    4.  
    5. public class Trigger_1 : MonoBehaviour
    6.  
    7. {
    8.  
    9.   public GameObject hazard;
    10.   public GameObject triggerArea;
    11.   public Vector3 spawnValues;
    12.   public int enemyCount = 1;
    13.  
    14.  
    15.   private void OnTriggerEnter(Collider triggerArea)
    16.   {
    17.   if (enemyCount > 0)
    18.   {
    19.  
    20.   Vector3 spawnPosition = new Vector3(spawnValues.x, spawnValues.y, spawnValues.z);
    21.   Quaternion spawnRotation = Quaternion.identity;
    22.   GameObject.Instantiate(hazard, spawnPosition, spawnRotation);
    23.   enemyCount--;
    24.   }
    25.   }
    26. }
    27.  
    28.  
     
  5. image28

    image28

    Joined:
    Jul 17, 2013
    Posts:
    457
    Instantiate is slow. At Start you should instantiate the max on screen enemies. Then turn the renderers on and off and move them when the are not needed. Would be much faster.
     
  6. Aldo

    Aldo

    Joined:
    Aug 10, 2012
    Posts:
    173