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

How to code something to happen when sending a command in a time range

Discussion in '2D' started by Dmumzo, Nov 30, 2019.

  1. Dmumzo

    Dmumzo

    Joined:
    Nov 28, 2019
    Posts:
    16
    I want to do the following:
    if I press "A" my character performs task 1.
    if I release "A", two things may happen:
    -if i press "D" before up to X seconds (so in a range between 0 and X seconds after releasing "A"), then my char performs task 2.
    -if X seconds have passed without the key "D" being pressed, then my char performs task 3.

    How do you code this? I have problems dealing with time on code.
     
  2. Olmi

    Olmi

    Joined:
    Nov 29, 2012
    Posts:
    1,553
    Hi @Dmumzo,

    You already wrote a functioning pseudo code logic, so what exactly you need help with?

    Usually when you want to do something with time and measure how much time has elapsed, you store a reference to the current time, and then when you check the elapsed time, you just
    subtract the time you stored from the current time.

    But this pattern works quite ok for simple scenarios. But if you're building something more complex like some combo system for a fighting game, then it needs more thinking etc.

    Don't forget to reset the timer after you have performed the time-dependent tasks.

    Here's some code I wrote quickly, it might work as a template, you need to change a few things for sure.

    Code (CSharp):
    1. public class InputBehavior : MonoBehaviour
    2. {
    3.     float timer;
    4.     float delay = 2f;
    5.  
    6.     void Update()
    7.     {
    8.         // If player presses A key,
    9.         if (Input.GetKeyDown(KeyCode.A))
    10.         {
    11.             Debug.Log("Player pressed A.");
    12.             // Store current time.
    13.             timer = Time.time;
    14.             // Perform initial task.
    15.             PerformTask1();
    16.         }
    17.  
    18.         // Every frame, calculate elapsed time.
    19.         float timeElapsed = Time.time - timer;
    20.         // If player presses D down and timer is less than or equal to the set delay,
    21.         if (Input.GetKeyDown(KeyCode.D) && timeElapsed <= delay)
    22.         {
    23.             Debug.Log("Player pressed D in time. time elapsed: " + timeElapsed);
    24.             PerformTask2();
    25.         }
    26.         // Else if player was too late, call another method.
    27.         else if (Input.GetKeyDown(KeyCode.D) && timeElapsed > delay)
    28.         {
    29.             Debug.Log("Player pressed D too late. time elapsed: " + timeElapsed);
    30.             PerformTask3();
    31.         }
    32.     }
    33.  
    34.     void PerformTask1()
    35.     {
    36.         Debug.Log("Perform task 1.");
    37.         // Do not reset timer, as we wait for next input in the sequence.
    38.     }
    39.  
    40.     void PerformTask2()
    41.     {
    42.         Debug.Log("Perform task 2.");
    43.         // Reset timer.
    44.         timer = 0;
    45.     }
    46.  
    47.     void PerformTask3()
    48.     {
    49.         Debug.Log("Perform task 3.");
    50.         // Reset timer.
    51.         timer = 0;
    52.     }
    53. }
     
    Last edited: Nov 30, 2019
    MisterSkitz likes this.
  3. Dmumzo

    Dmumzo

    Joined:
    Nov 28, 2019
    Posts:
    16
    Thank you!
    I had no idea this Time.time existed
     
  4. Olmi

    Olmi

    Joined:
    Nov 29, 2012
    Posts:
    1,553
    It's very useful for many cases, so add it to your toolbox :).