Hi all I am new bie to unity . In my project i have to setup time frame for the Mouse.
For eg. if twice mouse clicks "2 " should true
thrice and four clicks “3” “4” should true. I have a code for that.
using UnityEngine;
using System.Collections;
public class NewBehaviourScript19 : MonoBehaviour {
// Use this for initialization
private int clickCount = 0;
private float mouseTimer = 0;
public bool Thriceclick = false;
public bool Secondclick = false;
public bool firstclick = false;
//t += Time.deltaTime;
void Update()
{
if(Input.GetButtonDown("Fire1"))
{
if(mouseTimer > 0 && clickCount == 2)//Number of tabs you want minus one
{
firstclick = true;
}
if (mouseTimer > 0 && clickCount == 3 ) //Number of tabs you want minus one
{
Secondclick = true;
}
if (mouseTimer > 0 && clickCount == 4 )//Number of tabs you want minus one
{
Thriceclick = true;
}
else
{
mouseTimer = 0.8f;
clickCount += 1;
}
}
if(mouseTimer > 0)
{
mouseTimer -= 1 * Time.deltaTime;
}
else
{
clickCount = 0;
}
}
}
There is no problem for me. But my problem is
if the user clicked mouse twice within 2 seconds “2” should true.After the 2 condition becomes true if the user pressed 3 clicks within 3 seconds “3” should true.
after that 3 becomes true if the user clicked 4 seconds 4 clicks 4 th should true. Each and every one go through sequence. How can i set up the time frame?
how can i do it?
You might be having some float vs int issues - always put “f” after numbers that are float or expected to be used in float math. For example, on this line:
mouseTimer -= 1 * Time.deltaTime;
I’m fairly sure that “1 * Time.deltaTime” will give an int result of 0 - when multiplying int * float, it returns an int, and that means the number will get truncated. Therefore you end up just subtracting 0, and the mouseTimer never goes down.
The program flow - the way if/else are arranged there - is a little tough to follow too, but I think that that is your main issue.
Thanks Startmanta for you help. I have tried ur idea. But No Luck.Could any one help me. I need an example in any things.Mouse or keyboard.
In 10 Seconds.
If the user clicked or pressed 2 times in 2 seconds “2” should true.
if the user clicked or pressed 3 times in 3 seconds"3 " should true.
if the user clicked or pressed 4 times in 4 seconds “4” should true.
In my above coding. If i clicked 4 times "3 " & 4 also becomes true.
I have to avoid it. only seconds condition true only it should go for 3 condition .
I need an example for double click and triple click and four click.
at some point you’re going to need to decide how long a wait your going to give the user to click. You then want:
start time on the first click,
on all subsequent clicks, if the time delay isn’t up, add a click to the count
when the time delay is up, evaluate you’re clicks
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
public class Clicker : MonoBehaviour {
public float TimeLimit = 1;
private int ClickNo;
//For testing
public Text ClicksText;
IEnumerator ClickTimer()
{
yield return new WaitForSeconds(TimeLimit);
ClickNo = 0;//Reset clicks number
}
public int GetClicks()
{
return ClickNo;
}
// Update is called once per frame
void Update () {
if (Input.GetButtonDown("Fire1"))
{
ClickNo++;
//Restart timer
StopCoroutine("ClickTimer");
StartCoroutine("ClickTimer");
}
//Test
if (ClicksText)
{
ClicksText.text = ClickNo.ToString();
}
}
}
This script will increase the number of consecutive clicks in a determined period of time, and reset if more time have been passed. Then with another script you can use GetClicks() to obtain the current number of clicks and based on that value you can do whatever you want in your if statements.
Hi All I am Also new Bie to Unity. I am also find difficulty in set up the timer for mouse or keyboard. could any one can help me by giving source for set up time for keyboard or mouse. .
Thanks LeftyRighty & mich 9 for your code. @ mich 9 I have tried ur idea and enclose my code.
.
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
public class NewBehaviourScript12 : MonoBehaviour
{
public float TimeLimit = 1;
private int ClickNo;
//For testing
public Text ClicksText;
IEnumerator ClickTimer()
{
yield return new WaitForSeconds(TimeLimit);
ClickNo = 0;//Reset clicks number
}
public int GetClicks(int ClickNo)
{
if(ClickNo==2)
{
Debug.Log("Clicked 2");
StopCoroutine("ClickTimer");
StartCoroutine("ClickTimer");
}
if (ClickNo == 3)
{
Debug.Log("Clicked 3");
StopCoroutine("ClickTimer");
StartCoroutine("ClickTimer");
}
if (ClickNo == 4)
{
Debug.Log("Clicked 4");
StopCoroutine("ClickTimer");
StartCoroutine("ClickTimer");
}
return ClickNo;
}
// Update is called once per frame
void Update()
{
if (Input.GetButtonDown("Fire1"))
{
ClickNo++;
GetClicks(ClickNo);
//Restart timer
StopCoroutine("ClickTimer");
StartCoroutine("ClickTimer");
}
//Test
if (ClicksText)
{
ClicksText.text = ClickNo.ToString();
}
}
}
But still i have a problem. The wait for seconds is not working properly. My Code should go through sequence.
If the user clicked or pressed 2 times in 2 seconds “2” should true.
if the user clicked or pressed 3 times in 3 seconds"3 " should true.
if the user clicked or pressed 4 times in 4 seconds “4” should true.
In my above coding. If i clicked 4 times "3 " & 4 also becomes true.
I have to avoid it. only seconds condition true only it should go for 3 condition .
I am not sure if this is what you want, but do you want to have a code that detects how many clicks the user did (with a given delay between each click), and if that delay has passed handle that amount of clicks, or have it so each time you click you handle the current amount of clicks, and after a given delay if there is another click, you handle the new amount of cliks.
Ex : you want on triple click to get the debug on your console:
Clicked once, Clicked twice, Clicked third Time
or
Clicked third times
You are using my code wrong, it was intended to be modular, but anyway. Here is the code adapted to your needs:
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
public class Clicker : MonoBehaviour {
public float TimeLimit = 1;
private int ClickNo;
//For testing
public Text ClicksText;
IEnumerator ClickTimer()
{
yield return new WaitForSeconds(TimeLimit);
switch (ClickNo)
{
case 1:
Debug.Log("Clicked 1");
break;
case 2:
Debug.Log("Clicked 2");
break;
case 3:
Debug.Log("Clicked 3");
break;
case 4:
Debug.Log("Clicked 4");
break;
}
ClickNo = 0;//Reset clicks number
}
// Update is called once per frame
void Update () {
if (Input.GetButtonDown("Fire1"))
{
ClickNo++;
//Restart timer
StopCoroutine("ClickTimer");
StartCoroutine("ClickTimer");
}
//Test
if (ClicksText)
{
ClicksText.text = ClickNo.ToString();
}
}
}
If the user click 1 time the console will display “Clicked 1”, but if the user click 2 times it will skip the first one and display “Clicked 2”, the same for 3 and 4 clicks.
At the very least, I would suggest keeping a reference to the coroutine and using it instead of the string overload of Start/StopCoroutine. I’ve run into a lot of odd behavior when it comes to starting and stopping coroutines in succession like that.
You are right, I didn’t think about that. Then here the code fixed:
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
public class Clicker : MonoBehaviour {
public float TimeLimit = 1;
private int ClickNo;
private Coroutine ClickCoroutine;
//For testing
public Text ClicksText;
void Start()
{
ClickCoroutine = StartCoroutine(ClickTimer());
}
IEnumerator ClickTimer()
{
yield return new WaitForSeconds(TimeLimit);
switch (ClickNo)
{
case 1:
Debug.Log("Clicked 1");
break;
case 2:
Debug.Log("Clicked 2");
break;
case 3:
Debug.Log("Clicked 3");
break;
case 4:
Debug.Log("Clicked 4");
break;
}
ClickNo = 0;//Reset clicks number
}
// Update is called once per frame
void Update () {
if (Input.GetButtonDown("Fire1"))
{
ClickNo++;
//Restart timer
StopCoroutine(ClickCoroutine);
ClickCoroutine = StartCoroutine(ClickTimer());
}
//Test
if (ClicksText)
{
ClicksText.text = ClickNo.ToString();
}
}
}
Do you think it will be better to put all the logic in Update() instead of a coroutine? I mean, the coroutine will execute only when is needed eg: after the first click, but if I put the code in Update() it will always run, even if is not being used.
You have code in Update that’s always run too. The Input check is the same so the overhead when not being used is identical. Also - your code never resets the click count back to 1. And you could skip the switch statement entirely with just this
Debug.Log("Clicked " + ClickNo);
Lastly - you should only update the UI text when the user clicks, not every frame.
Again - I mentioned this method because I’ve had issues starting and stopping coroutines in rapid succession. The only thing the coroutine is buying you here is a convenient method for waiting an allotted amount of time - something my way accomplishes just by checking against Time.time.
But you will not put the code to decrease the timer inside the input check, it have to be outside hence is always running, but let’s not talk about it, I think a simple timer in Update() will not hurt performance.
My code doesn’t need to reset the click count back to 1, but back to 0 that means the user have made no clicks:
IEnumerator ClickTimer()
{
yield return new WaitForSeconds(TimeLimit);
switch (ClickNo)
{
case 1:
Debug.Log("Clicked 1");
break;
case 2:
Debug.Log("Clicked 2");
break;
case 3:
Debug.Log("Clicked 3");
break;
case 4:
Debug.Log("Clicked 4");
break;
}
ClickNo = 0;//Reset clicks number
}
And about:
Debug.Log("Clicked " + ClickNo);
My previous code was optimized for that, but the OP wanted a Switch statement, so I did it.
And about the UI text is just for testing purposes, he will not need it anyway.
After all I just giving to the OP some hints about how to solve his problem, now it depends on him to adapt it and optimize it to his needs.
My code does nothing with decreasing a timer. It checks the current time against the last time the user clicked to see if it’s within a given threshold. None of that happens without the user clicking.