[SOLVED] (2D) Splitter Enemy?

I’m planning for my game to have an enemy that splits into two weaker one when the original gets killed, kind of like a goo enemy. I’m kind of confused on how to do this and I need some help.

Thanks!

If you have your bigger enemy working already, make another prefab that contains a smaller version of it (maybe tweak the combat values, too) and make a reference to the smaller one in the larger one’s behavior script. Just before destroying the larger one when it runs out of health or is hit, instantiate two of the smaller ones nearby. You could also give them an initial velocity to make them appear to pop out of the position of the larger one.

GameObject smallSplitter; //Set this reference in the inspector

...

//In the code to kill the enemy
Instantiate(smallSplitter, transform.position, transform.rotation);
Instantiate(smallSplitter, transform.position, transform.rotation);
Destroy(gameObject);

You can modify the spawn position from transform.position so that they appear in slightly different spots, if you want. You can add an initial velocity to each like this:

GameObject smallSplitter; //Set this reference in the inspector
Vector2 splitVel1; //Set value in inspector
Vector2 splitVel2; //Set value in inspector

...

//In the code to kill the enemy
GameObject smlSplt1 = Instantiate(smallSplitter, transform.position, transform.rotation);
GameObject smlSplt2 = Instantiate(smallSplitter, transform.position, transform.rotation);

smlSplt1.GetComponent<Rigidbody>().velocity = splitVel1;
smlSplt2.GetComponent<Rigidbody>().velocity = splitVel2;
Destroy(gameObject);

Look into object pooling if you are going to have a lot of these, especially if it’s a mobile-deployed project.

Cool, thanks for the help!

What if you wanted it to spit into another enemy with different traits looks blah blah blah…
I am trying to make a scorpion split into 2 crabs, and each crab splits into 3 Warriors, the Warriors spawn 2 Basics, and the basics spawn 5 swarms. I am not sure if i can do this with tags, or should I do it with just the specific name of the sprite?

How would you tackle it?