If Time > 0.25s
Hey, I have this C# script with boolean applied to it (i don't know if that's relevant) and it's basically just "something happens when you hold left mouse button down", but it bothers me that it happens everytime I click. So I'd like to know how to make this script on Input.GetMouseButton(0) happen only if Input.GetMouseButton(0) is held more than let's say 0.25s.
I wouldn't mind if somebody knows a better way to solve this problem. I'd really appreciate any help.
Answer by Denvery · Sep 30, 2015 at 05:36 PM
Hello, Laggy!
I think you can use code like this for your purpose:
private bool _isHolding = false;
private const float TargetDelay = 0.25f;
private float _holdingTime = 0;
private void CheckForMouseButton()
{
if (Input.GetMouseButton(0))
{
_holding = true;
_holdingTime += Time.deltaTime;
if (_holdingTime > TargetDelay)
{
PerformYourAction();
_isHolding = false;
_holdingTime = 0;
}
}
else
{
_isHolding = false;
_holdingTime = 0;
}
}
Your answer
![](https://koobas.hobune.stream/wayback/20220612035553im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
SqrMagnitude intermittently doesn't work for if statement on prefab. (C sharp) 1 Answer
I want to create a button to check if one skybox is enable and change it to another one. 0 Answers
Increase the time in X seconds each time the score equals a multiple of 500 1 Answer
If statement for direction the character is facing 0 Answers