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.
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
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.
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:
using UnityEngine;
using System.Collections;
public class Trigger_1 : MonoBehaviour
{
public GameObject hazard;
public GameObject triggerArea;
public Vector3 spawnValues;
public int enemyCount = 1;
private void OnTriggerEnter(Collider triggerArea)
{
if (enemyCount > 0)
{
Vector3 spawnPosition = new Vector3(spawnValues.x, spawnValues.y, spawnValues.z);
Quaternion spawnRotation = Quaternion.identity;
GameObject.Instantiate(hazard, spawnPosition, spawnRotation);
enemyCount--;
}
}
}
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.