- Home /
 
Countdown and Gameover,Game over based on collider and timer
Hey everyone, I need some help. In the project I'm working on there will be 3 rooms. I want each one to have a timer and when the timer runs off and the player is still in the room, it's game over. How do I do that code-wise?,Hey community, I want to make something and I'm really no sure how. In my game I want each room to have a countdown and If the player is still in the room after the countdown, it's game over. Any ideas on how to do that code-wise?
Answer by Kciwsolb · Apr 18, 2018 at 06:59 PM
First of all, you would want 3 separate GameObjects with trigger box colliders set up in the editor. These would be your rooms.
On each room GameObject you would want a copy of a script that looks something like this:
 Using System.Collections;
 Using System.Collections.Generic;
 Using UnityEngine;
 
 public class EscapeRoom : MonoBehaviour //Or what ever you want to call it
 {
         public float escapeTime; //How long you have to escape that room
 
         private float endTime;
         private bool started;
 
         private void OnTriggerEnter(Collider other)
         {
                 if(other.gameObject.CompareTag("Player")) //Make sure player GameObject is tagged right
                 {
                         endTime = Time.time + escapeTime;
                         started = true;
                 }
         }
         private void OnTriggerExit(Collider other)
         {
                 if(other.gameObject.CompareTag("Player")) //Make sure player GameObject is tagged right
                 {
                         started = false;
                 }
         }
 
         private void Update()
         {
                  if(started)
                  {
                          if(Time.time >= endTime)
                          {
                                 //Game Over!
                          }
                  }
         }
 }
 
               If this helps please let me know by marking it as the answer! :) Good luck!
Your answer