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

Fast Click, Slow Click

Discussion in 'Scripting' started by Raclette, Mar 28, 2016.

  1. Raclette

    Raclette

    Joined:
    Oct 16, 2012
    Posts:
    15
    Hello everyone I need some advice for a new game.

    I want my character to run when I click fast multiple times anywhere on the screen. To walk when I click slow multiple times and to stop when I don't click.

    Click Click Click Click Click = run;
    Click ----- Click ----- Click = walk;
    No Click = stop;

    I believe that I need a Counter of Click and a function that calculate the time between each Click but I don't know how to make it.

    Can you help me?

    Thanks
     
  2. SubZeroGaming

    SubZeroGaming

    Joined:
    Mar 4, 2013
    Posts:
    1,008
    you'll need to record the time since click compared to the time since last click. If it's less than 500ms then you should run.

    Attempt it yourself and paste your code, and we'll guide you.
     
  3. Raclette

    Raclette

    Joined:
    Oct 16, 2012
    Posts:
    15
    Ok thanks I will try.
     
  4. Raclette

    Raclette

    Joined:
    Oct 16, 2012
    Posts:
    15
    Hi again @SubZeroGaming I tried something but it’s not working very well. I’m new in coding so please don’t be too hard on me. :)
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class NewBehaviourScript : MonoBehaviour {
    5.  
    6.     //public float timeCounter;
    7.     private bool firstClick;
    8.     private bool secondClick;
    9.     private bool startTimeCounter;
    10.     private float timeCounter;
    11.     private float delay;
    12.     private float timeBetweenClicks;
    13.  
    14.  
    15.     // Use this for initialization
    16.     void Start () {
    17.         firstClick = true;
    18.         secondClick = false;
    19.         startTimeCounter = false;
    20.         delay = 1f;
    21.     }
    22.    
    23.     // Update is called once per frame
    24.     void Update () {
    25.  
    26.  
    27.    
    28.         if (Input.GetMouseButtonDown (0) && firstClick == true)
    29.         {
    30.             firstClick = false;
    31.             secondClick = true;
    32.             timeCounter = 0f;
    33.             startTimeCounter = true;
    34.         }
    35.         else if (Input.GetMouseButtonDown (0) && secondClick == true)
    36.         {
    37.             firstClick = true;
    38.             secondClick = false;
    39.             startTimeCounter = false;
    40.         }
    41.  
    42.         if (startTimeCounter)
    43.         {
    44.             timeCounter += Time.deltaTime;
    45.         }
    46.  
    47.         if (timeCounter > delay)
    48.         {
    49.             startTimeCounter = false;
    50.             timeCounter = 0;
    51.             timeBetweenClicks = 0;
    52.         }
    53.  
    54.         timeBetweenClicks = timeCounter;
    55.  
    56.         if (timeBetweenClicks == Mathf.Clamp (timeBetweenClicks, 0.1f, 0.2f))
    57.         {
    58.             Debug.Log ("Run");
    59.         }
    60.         else if (timeBetweenClicks == Mathf.Clamp (timeBetweenClicks, 0.2f, 1f))
    61.         {
    62.             Debug.Log ("Walk");
    63.         }
    64.         else if (timeBetweenClicks < 0.1f)
    65.         {
    66.             Debug.Log ("Stop");
    67.         }
    68.            
    69.     }
    70. }
    71.  
     
  5. SubZeroGaming

    SubZeroGaming

    Joined:
    Mar 4, 2013
    Posts:
    1,008
    Thanks for attempting it. Now that you at least showed the community you're willing to learn and develop on your own without being spoon fed, i can help you out :)

    So, let's break this problem out. You want to basically determine if the time between the first click and the second click is within say 0.5 seconds. If it is, you're running. If it's longer, you're walking.

    So what do we need to record? We need to record the time of the next click, and compare it to the last click.

    You have more variables then you need.

    You need a variable to record the time of the current click right? So create a float variable and call it nextClick.

    When you left click, you need to assign the next Click variable to the current time. You can do that by Time.Time. Time.time is how long the game has been running.

    now that you have recorded the current clicks time, you need to compare it to the last clicked time. So you need another variable, call it lastClick. (also a float)

    Before tracking your nextclicks time, you'll want to set the lastClick = to nextClick.

    Here's an example showing why:

    float lastClick = -1f; //initialize it to negative 1 cause it never happened yet.
    float nextClick = 0f;

    say you left click 2 seconds into the game.

    here's what needs to happen.

    lastClick = nextClick which is 0
    nextClick = Time.time which is 2.

    now you need to calculate the timeDifference between the currentClick (next) and the lastClick.

    float timeDiff = nextClick - lastClick = 2

    now check if timeDiff is < than your running threshold,.(0.5f)

    if it is, you are running, else you are walking.

    Here's some pseudo code. Try figuring it out.



    Code (CSharp):
    1. nextClick varaible
    2. lastClick variable
    3.  
    4. Update()
    5.  
    6. //if left mouse click
    7. //lastClick = nextClick
    8. //nextClick = currenTime (time.time)
    9.  
    10. //calculate the time difference (store in a local variable)
    11.  
    12. //if timeDiff < 0.5f
    13. // debug Run!
    14. //else
    15. //Debug walk!
    Comment if you have problems,.
     
  6. mahadevank

    mahadevank

    Joined:
    Feb 29, 2016
    Posts:
    21
    Try this:

    Code (CSharp):
    1.     public class NewBehaviourScript :MonoBehaviour {
    2.         private float delay=0.0f;
    3.  
    4.         void Update() {
    5.             if(Input.GetMouseButtonDown(0)) {
    6.                 if(delay>0.5)
    7.                     Debug.Log("Walk!");
    8.                 else
    9.                    Debug.Log("Run");
    10.                 delay=0.0f;
    11.             }
    12.             delay=delay+Time.deltaTime;
    13.         }
    14.     }
    Syntax and stuff could be a little off since I didn't write this in Unity, but it should work
     
    Last edited: Mar 29, 2016
    GarthSmith likes this.
  7. SubZeroGaming

    SubZeroGaming

    Joined:
    Mar 4, 2013
    Posts:
    1,008
    What are you doing? Stop spoon feeding, and that's a S***ty way of doing it. You don't need a delay variable and nothing with time.deltaTime.
     
  8. Raclette

    Raclette

    Joined:
    Oct 16, 2012
    Posts:
    15
    Thanks a lot @SubZeroGaming I tried your way but must have forgotten something because It won’t stop running... Can you help me without a spoon. :)
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class NewBehaviourScript : MonoBehaviour {
    5.  
    6.     private float nextClick;
    7.     private float lastClick;
    8.     private float timeDiff;
    9.     private float currentTime;
    10.  
    11.  
    12.     void Update () {
    13.  
    14.         currentTime = Time.time;
    15.  
    16.  
    17.         //if left mouse click
    18.         if(Input.GetMouseButtonDown(0))
    19.             {
    20.             //lastClick = nextClick
    21.             lastClick = nextClick;
    22.             //nextClick = currenTime (time.time)
    23.             nextClick = currentTime;
    24.  
    25.             }
    26.  
    27.         //calculate the time difference (store in a local variable)
    28.         timeDiff = nextClick - lastClick;
    29.  
    30.  
    31.         //if timeDiff < 0.5f
    32.         if (timeDiff < 0.5f)
    33.         {
    34.             // debug Run!
    35.             Debug.Log("Run");
    36.         }
    37.         else
    38.         {
    39.             Debug.Log ("Walk");
    40.         }
    41.  
    42.     }
    43. }
     
  9. Raclette

    Raclette

    Joined:
    Oct 16, 2012
    Posts:
    15
    Oh sorry I know why !
    float lastClick = -1f; //initialize it to negative 1 cause it never happened yet.
    float nextClick = 0f;
    Thank !!!
     
  10. SubZeroGaming

    SubZeroGaming

    Joined:
    Mar 4, 2013
    Posts:
    1,008
    You're welcome. Good job :)