Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Как сделать перезапуск уровня при нажатии пробела в течение 3 секунд?

Discussion in '2D' started by levap1818, Feb 5, 2020.

  1. levap1818

    levap1818

    Joined:
    Feb 5, 2020
    Posts:
    2
    Помогите пожалуйста с скриптом для перезапуска уровня при нажатии клавиши (в моем случае пробела) в течение какого-то количества времени.
     
  2. eses

    eses

    Joined:
    Feb 26, 2013
    Posts:
    2,637
  3. hippocoder

    hippocoder

    Digital Ape

    Joined:
    Apr 11, 2010
    Posts:
    29,723
    A possible translation:
     
  4. eses

    eses

    Joined:
    Feb 26, 2013
    Posts:
    2,637
    @levap1818

    If Hippocoder translated your question correctly, you could try something like this to load your scene if space bar (for example) is pressed for 3 seconds:
    Code (CSharp):
    1. // In your class
    2. float timer;
    3. float maxTime = 3;
    4.  
    5. // In your update
    6. if (Input.GetKey(KeyCode.Space))  
    7. {
    8.     timer += Time.deltaTime;
    9.  
    10.     if (timer > maxTime)
    11.     {
    12.         Debug.Log("Reloading scene");
    13.         SceneManager.LoadScene(SceneManager.GetActiveScene().name);
    14.     }
    15. }
    16. else
    17. {
    18.     timer = 0;
    19. }
     
    WindGess, levap1818 and hippocoder like this.
  5. levap1818

    levap1818

    Joined:
    Feb 5, 2020
    Posts:
    2
    Thanks!
     
  6. WindGess

    WindGess

    Joined:
    Dec 29, 2021
    Posts:
    1
    thank you very much brother