code so if 1 enemy isnt killed every 4 seconds player dies?
how do I code it so if 1 enemy isn't killed at least every 4 seconds the player dies?
not sure if this is right code to put here but this script is on all the enemies enemies are instantiated every second at least this is the enemys die script
using UnityEngine;
using System.Collections;
[RequireComponent(typeof(AudioSource))]
public class dedd : MonoBehaviour {
public AudioClip pop1;
public AudioClip boing;
AudioSource audio;
Animator anim; //reference to the animator component
void Start()
{
audio = GetComponent<AudioSource>();
//get reference to the animator component
anim = GetComponent<Animator> ();
}
void OnCollisionEnter2D(Collision2D other)
//void OnTriggerEnter2D(Collider2D other)
{
if (other.gameObject.tag == "Player" || other.gameObject.tag == "Wall" )
{
// do something when we collide with "enemy1" OR "enemy2"
anim.Play("fishded");
anim.SetTrigger("Die");
GetComponent<AudioSource> ().Play ();
audio.PlayOneShot(pop1, 0.7F);
}
else if (other.gameObject.tag == "bad" || other.gameObject.tag == "bad" )
{
// do something else when we collide with "enemy3" OR "enemy4"
anim.Play("fishded");
anim.SetTrigger("Die");
GetComponent<AudioSource> ().Play ();
audio.PlayOneShot(boing, 0.7F);
}
}
}
Answer by NoseKills · Feb 03, 2017 at 05:18 PM
Quick and dirty:
public class DeathWatch : MonoBehaviour {
private const float DeathTimeLimit = 4;
private static float deathCountdown = DeathTimeLimit;
private void Update() {
deathCountDown -= Time.deltaTime;
if (deathCountdown < 0) {
// Game over
}
}
public static void ReportDeath() {
deathCountdown = DeathTimeLimit;
}
}
Have just 1 object with this script in the scene, call DeathWatch.ReportDeath() when an enemy dies and handle game over somehow
I added this to the player but nothing is happening
how do I call DeathWatch.ReportDeath() ? what does that mean?
im a newbie
for this to work for now do I have to setup the game over function ?
its not killing the player nothing is happening so far
So where ever you detect an enemy dying, you need to call DeathWatch.ReportDeath() ;
. Just write that piece of text to where you detect an enemy dying in your code.
If you look at my script, you can see it's just a 4 second countdown timer and every time you call ReportDeath the countdown resets.
The acript doesn't do anything yet when that 4 seconds expires: // Game Over
is just a comment that shows where you have to put your code for handling game over.
its not working heres the script
public class DeathWatch : $$anonymous$$onoBehaviour {
public AudioClip pop1;
public AudioClip boing;
AudioSource audio;
Animator anim;
private const float DeathTimeLimit = 4;
private static float DeathCountdown = DeathTimeLimit;
void Start()
{
audio = GetComponent<AudioSource>();
//get reference to the animator component
anim = GetComponent<Animator> ();
}
private void Update() {
DeathCountdown -= Time.deltaTime;
if (DeathCountdown < 0) {
anim.Play("fishded");
anim.SetTrigger("Die");
audio.PlayOneShot(pop1, 0.7F);
// Game over
}
}
public static void ReportDeath() {
DeathCountdown = DeathTimeLimit;
}
}