Detect when a key is pressed or not pressed using Corutine?
Hi everyone! I'm fairly new to C# and I'm trying to write my script for enemy AI. What I want is if the "S" key is pressed the enemy goes away (Gets destroyed). If no key is pressed for a certain amount of time the game resets. Instead the enemy Debug.Log reset comes up at the start (which makes sense because it is in my start function. However, if I put it under update it updates every frame. Should I use FixedUpdate instead? I need a clear example of how this should go.
public class EnemyAI : MonoBehaviour { public int waitTime = 1; public int fadeTime = 3;
public GameObject enemy;
void Start ()
{
StartCoroutine (FadeIn ());
if (!Input.GetKeyDown (KeyCode.S)) {
StartCoroutine (ResetEnemy ());
}
}
void Update ()
{
if (Input.GetKeyDown (KeyCode.S)) {
Debug.Log ("Death Enemy Started");
//Fade out
SetMaterialTransparent ();
iTween.FadeTo (enemy, 0, 1);
Destroy (GameObject.FindWithTag ("Enemy"));
}
}
IEnumerator FadeIn ()
{
Debug.Log ("Enemy Has Entered");
yield return new WaitForSeconds (5);
// Fade in
iTween.FadeTo (enemy, 1, 1);
Invoke ("SetMaterialOpaque", 1f);
}
IEnumerator ResetEnemy ()
{
Debug.Log ("Reset Game");
yield return new WaitForSeconds (30);
//Fade out
SetMaterialTransparent ();
iTween.FadeTo (enemy, 0, 1);
Destroy (GameObject.FindWithTag ("Enemy"));
}
}
Your answer
Follow this Question
Related Questions
Coroutine skips wait for seconds 0 Answers
Coroutine not working on android build 1 Answer
Coroutines not running one after another 1 Answer
StopCoroutine() Not Working 2 Answers
Click two buttons at the same time 0 Answers