- Home /
How to make two enemies appear in sequence?
The game is shooting game, i need the second enemy appear only when the first one is hit, and the first one reappears again after the second one is hit. And every one stays for 3 seconds, i know it seems a coroutine issue, but i can not handle it, any help please?
Please provide code of what you are currently doing.
I'm also a little confused, let me see if I understand:
You want enemy1 to spawn
You want enemy2 to spawn only after enemy1 is hit.
You want enemy1 to respawn if enemy2 is hit.
This is where I'm confused: You want everything to appear for 3 seconds? Does this override if someone was hit? What do you mean "And every one stays for 3 seconds"? This statement seems to contradict the previous three I listed above the way I'm understanding this right now.
@I_Am_Err00r I mean if the enemy is hit before 3 seconds it will disappear, if not hit it will wait till 3 seconds then disappear and the other enemy appears.
Here is the code:
public class Target : $$anonymous$$onoBehaviour { public int position;
public GameObject activeRobot;
[SerializeField]
private GameObject[] robots;
[SerializeField]
private void OnCollisionEnter(Collision collision)
{
Destroy(collision.collider.gameObject);
activeRobot.GetComponent<Animator>().Play("Die");
game.AddHit();
GetComponent<BoxCollider>().enabled = false;
activeRobot = null;
StartCoroutine("AliveTimer");
StartCoroutine("DeathTimer");
}
public void ActivateRobot()
{
activeRobot = robots[Random.Range(0,3)];
activeRobot.SetActive(true);
activeRobot.GetComponent<Animator>().Play("Rise");
GetComponent<BoxCollider>().enabled = true;
}
public void ActivateRobot(RobotTypes type)
{
StopAllCoroutines();
activeRobot = robots[(int)type];
activeRobot.SetActive(true);
activeRobot.GetComponent<Animator>().Play("Rise", 0, 1);
GetComponent<BoxCollider>().enabled = true;
}
IEnumerator AliveTimer()
{
yield return new WaitForSeconds (3);
ActivateRobot();
}
IEnumerator DeathTimer()
{
yield return new WaitForSeconds(Random.Range(10, 14));
if (activeRobot == null)
{
yield break;
}
activeRobot.GetComponent<Animator>().Play("Die");
GetComponent<BoxCollider>().enabled = false;
activeRobot = null;
StartCoroutine("AliveTimer");
}
}
Answer by m0mohy92 · Jul 30, 2019 at 02:55 PM
@I_Am_Err00r I mean if the enemy is hit before 3 seconds it will disappear, if not hit it will wait till 3 seconds then disappear and the other enemy appears.
Here is the code:
private void OnCollisionEnter(Collision collision)
{
Destroy(collision.collider.gameObject);
activeRobot.GetComponent<Animator>().Play("Die");
game.AddHit();
GetComponent<BoxCollider>().enabled = false;
activeRobot = null;
StartCoroutine("AliveTimer");
StartCoroutine("DeathTimer");
}
public void ActivateRobot()
{
activeRobot = robots[Random.Range(0,3)];
activeRobot.SetActive(true);
activeRobot.GetComponent<Animator>().Play("Rise");
GetComponent<BoxCollider>().enabled = true;
}
public void ActivateRobot(RobotTypes type)
{
StopAllCoroutines();
activeRobot = robots[(int)type];
activeRobot.SetActive(true);
activeRobot.GetComponent<Animator>().Play("Rise", 0, 1);
GetComponent<BoxCollider>().enabled = true;
}
IEnumerator AliveTimer()
{
yield return new WaitForSeconds (3);
ActivateRobot();
}
IEnumerator DeathTimer()
{
yield return new WaitForSeconds(Random.Range(10, 14));
if (activeRobot == null)
{
yield break;
}
activeRobot.GetComponent<Animator>().Play("Die");
GetComponent<BoxCollider>().enabled = false;
activeRobot = null;
StartCoroutine("AliveTimer");
}
Is your code compiling? I see you have two methods of the same name:
public void ActivateRobot()
{
activeRobot = robots[Random.Range(0,3)];
activeRobot.SetActive(true);
activeRobot.GetComponent<Animator>().Play("Rise");
GetComponent<BoxCollider>().enabled = true;
}
public void ActivateRobot(RobotTypes type)
{
StopAllCoroutines();
activeRobot = robots[(int)type];
activeRobot.SetActive(true);
activeRobot.GetComponent<Animator>().Play("Rise", 0, 1);
GetComponent<BoxCollider>().enabled = true;
}
Turns out you're right, as long as return type is different it will work, I guess I've never named a method the exact same with a different return type; that can be very confusing for anyone besides you and anyone else who knows the code backwards and forwards, I would consider changing one of them to avoid that confusion in the future; regardless, I never see:
public void ActivateRobot(RobotTypes type)
{
StopAllCoroutines();
activeRobot = robots[(int)type];
activeRobot.SetActive(true);
activeRobot.GetComponent<Animator>().Play("Rise", 0, 1);
GetComponent<BoxCollider>().enabled = true;
}
getting called within this script, is that getting called in another?
However, I would probably go about all this like this:
float time;
//can be public or private
public void Update()
{
time += Time.deltaTime()
if(time > 3f)
{
ActivateRobot();
}
}
private void OnCollisionEnter(Collision collision)
{
Destroy(collision.collider.gameObject);
activeRobot.GetComponent<Animator>().Play("Die");
game.AddHit();
GetComponent<BoxCollider>().enabled = false;
activeRobot = null;
ActivateRobot();
StartCoroutine("DeathTimer");
}
public void ActivateRobot()
{
activeRobot = robots[Random.Range(0,3)];
activeRobot.SetActive(true);
activeRobot.GetComponent<Animator>().Play("Rise");
GetComponent<BoxCollider>().enabled = true;
time = 0; //I don't know if you want that 3 second timer to reset every time, but if you don't just add this before time = 0: if(time < 3) { return;} or something like that.
}
public void ActivateRobot(RobotTypes type)
{
StopAllCoroutines();
activeRobot = robots[(int)type];
activeRobot.SetActive(true);
activeRobot.GetComponent<Animator>().Play("Rise", 0, 1);
GetComponent<BoxCollider>().enabled = true;
}
IEnumerator AliveTimer()
{
yield return new WaitForSeconds (3);
ActivateRobot();
}
IEnumerator DeathTimer()
{
yield return new WaitForSeconds(Random.Range(10, 14));
if (activeRobot == null)
{
yield break;
}
activeRobot.GetComponent<Animator>().Play("Die");
GetComponent<BoxCollider>().enabled = false;
activeRobot = null;
StartCoroutine("AliveTimer");
}
Your answer
Follow this Question
Related Questions
Making Coroutine Repeat? 3 Answers
WaitForSeconds behaves weirdly 1 Answer
Enemy Damage problem 1 Answer
Char shots oneside only / kills enemy on specific place. 0 Answers