Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Timer and level restart Help plz

Discussion in 'Scripting' started by ToxicAntimater, Jun 6, 2016.

  1. ToxicAntimater

    ToxicAntimater

    Joined:
    May 11, 2016
    Posts:
    6
    I am make a game that is similar to Super Monkey Ball Deluxe and I need a countdown timer and once that timer runs out the level restarts. I have been looking around for a while and cant find anything. also if anyone can help me with my terrain controller it would be much appreciated.

    Currently I'm using this code:

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class TerrainController : MonoBehaviour {
    5.  
    6.     // Use this for initialization
    7.     void Start () {
    8.    
    9.     }
    10.    
    11.     // Update is called once per frame
    12.     void Update () {
    13.    
    14.     float z = Input.GetAxis("Horizontal") * -15.0f;
    15. Vector3 euler = transform.localEulerAngles;
    16. euler.z = Mathf.Lerp(euler.z, z, 50.0f);
    17. transform.localEulerAngles = euler;
    18.  
    19.     float x = Input.GetAxis("Vertical") * 15.0f;
    20. Vector3 reuler = transform.localEulerAngles;
    21. reuler.x = Mathf.Lerp(reuler.x, x, 50.0f);
    22. transform.localEulerAngles = reuler;
    23.  
    24.     }
    25. }
    on a cube that you can't collide with or see and making the cubes the ball rolls over a child of that object, however, it is very jumpy and you can glitch through the floor quite easily.
     
  2. LeftyRighty

    LeftyRighty

    Joined:
    Nov 2, 2012
    Posts:
    5,148
    Really? because googling "unity countdown timer" and "unity restart level" comes up with loads of results and vids. Just be careful with the "LoadLevel" function, there were some changes done recently and it's moved from "Application.LoadLevel(...)" to SceneManager.LoadLevel(...)". Both currently work, but use the newer sceneManager version.


    huh?
     
  3. traderain

    traderain

    Joined:
    Jul 7, 2014
    Posts:
    108
    Since you are using c# you can just launch a stopwatch and when that ends reload the level.
    So you start a timer on void Start();
    and in update check for it to end and reload the level.
    Code (CSharp):
    1. Using System.Diagnostics;
    2. SceneManager.LoadScene(1);
    3. Stopwatch a = new Stopwatch();
     
  4. nelson218

    nelson218

    Joined:
    Jun 7, 2016
    Posts:
    11
    for timer you can use something like these
    Code (CSharp):
    1. var timeLimit = 60.0f//1 min
    2. var currentTime = .0f;
    3.  
    4. void Start()
    5. {
    6.    InvokeRepeating("IncreaseTime",0, .1f);
    7. }
    8.  
    9. void IncreaseTime()
    10. {
    11.    currentTime += .1f;
    12.  
    13.   if(currentTime > timeLimit )
    14.   {
    15.       //time up
    16.   }
    17. }
     
    traderain likes this.