Okay you have two options.
I'll write and advice before writing options. Instead of controlling enemy count on every enemy object, **create an EnemyController.cs** script and **add an empty gameobject called Enemy_Manager**. This script should contain the information of how many enemies left on your game. The following script considers **all of your enemies has "Enemy" tag.**
**Enemy Controller Class**[c#]
public int enemyCount;
GameObject enemies[];
void Start()
{
enemies = GameObject.FindGameObjectsWithTag("Enemy");
enemyCount = enemies.Length;
}
public void ModifyEnemyCount()
{
enemyCount --;
if (enemyCount == 0)
{
//DoSomething.
}
}
After then if your enemies die, they should decrease this value. So you should modify your **EnemyDead.js** script as:
function OnCollisionEnter( collision : Collision )
{
if(collision.gameObject.tag == "fireball")
{
animation.Play("dying");
audio.Play();
yield WaitForSeconds(0.2);
GameObject.Destroy ( gameObject ) ;
var enemyCont : EnemyController = GameObject.Find("Enemy_Manager").GetComponent();
enemyCont.ModifyEnemyCount();
}
This way you now have a centralised enemy control system that you can do other things too. Now to your problem' solution.
First Option:
Add your boss before you start the game, add the waypoints and disable his GameObject. When all the enemies are defeated, enable his GameObject. So basically what you want to do is on your **ModifyEnemyCount** method, you want to enable your boss's gameobject. So;
public void ModifyEnemyCount()
{
enemyCount --;
if (enemyCount == 0)
{
GameObject boss = GameObject.Find("boss");
boss.enabled = true;
}
}
Second Option:
You need a waypoint manager just like enemy manager we did back there. So **add an empty gameobject called Waypoint_Manager** and **attach a script called WaypointController**.
**Waypoint Controller Class**[c#]
public Transform[] waypoints;
Attach your waypoints to this object and do the following within the **EnemyMovementController.cs** script:
public class EnemyMovementController : MonoBehaviour
{
// speed of the AI player
public int speed = 4;
// speed the ai player rotates by
public int rotationSpeed = 5;
// the waypoints
Transform[] waypoints; // NO NEED FOR THIS TO BE PUBLIC ANYMORE
// current waypoint id
private int waypointId = 0;
// the character controller component
private CharacterController characterController;
private Transform player;
public int chasingDistance = 30;
private bool attacking = false;
WaypointController waypointScript; //TO REFERENCE OUR NEW SCRIPT
/**
Init
*/
void Start()
{
// retrieve this GameObjects CharacterController Component and store in characterController (todo)
characterController = gameObject.GetComponent();
player = Motor.Instance.transform;
waypointScript = GameObject.Find("Waypoint_Manager").GetComponent(); // get the WaypointController script on Waypoint_Manager gameobject within our waypointScript variable.
waypoints = waypointScript.waypoints; // assign our centralised waypoints from WaypointController to enemies' waypoints.
}
Note that I didn't test the scripts, just wrote them here, there might be some errors but you should be able to fix them if happens. If not, please ask again.
I hope this helps.
↧