I’m making a 2D platformer and I’m trying to get my player to start a boss battle by passing a box collider which should instantiate the boss and then stop the player leaving the area. My problem is that i dont know how to get the collider to stop me from leaving and when the trigger activates, multiple boss characters appear when i just want one. Here’s the code
First of all, you should avoid using hardcoded values in the code. You should not need to have “151.9061” in the code. Instead, refer to the position of another existing object.
Instead of playing with “IgnoreCollision”, I suggest you play with isTrigger. Make your prefab a trigger at creation, and change it to a collider when the player has exited it, so it will not be able to pass it.
As for the boss issue: to avoid multiple bosses instantiations, you can use a boolean:
// Define this bool as a class variable
private bool isBossSpawned = false;
void OnTriggerExit2D(Collider2D o)
{
instantiateBoss();
barrier.isTrigger = false;
}
private void instantiateBoss()
{
if (isBossSpawned)
return;
Instantiate (boss, startPos, Quaternion.Euler (new Vector3 (0, -180, 0)));
isBossSpawned = true;
}