Detect mouse press and release
I want to detect if the user pressed the mouse button and released it within one second.
Example: The user holds down the left mouse button for more than a second. => function returns false.
The user holds down the left mouse button and releases in less than a second. => function returns true;
On mouse button down store the current time (Time.time) in a float variable.
On mouse button up compare the current time with the stored value.
If the difference is less than 1 you have what you're looking for.
Answer by LuckierBread · Sep 02, 2016 at 08:12 PM
Hi,
I'm no pro but I would go about this by using Input.GetMouseButtonDown. it works by making a bool true as long as the mouse button is pressed down. I would then attach a timer to that so that when the button is pressed it starts a clock then when the bool turns false stop the timer and then use an if else function to decide to make an output true or false.
you can check this out if you have any other questions about Input.GetMouseButtonDown https://docs.unity3d.com/ScriptReference/Input.GetButtonDown.html
Remember to pick this as the best answer and upvote if this helped. =)
This is what I though but I can't figure out how to use a timer.
Here bud wrote a script just for you
private float clickStart;
private bool moreThenOne;
void Update ()
{
if (Input.Get$$anonymous$$ouseButtonDown(0))
{
clickStart = Time.time;
}
if (Input.Get$$anonymous$$ouseButtonUp(0) && Time.time - clickStart >= 1)
{
moreThenOne = true;
}
if (Input.Get$$anonymous$$ouseButtonUp(0) && Time.time - clickStart < 1)
{
moreThenOne = false;
}
}
Answer by SuhaimKhalid · Jul 20, 2020 at 09:35 PM
Just change if (Input.GetMouseButtonUp(1)) { do whatever }
Your answer
Follow this Question
Related Questions
Limited response from mouse input in game view. 2019.3 0 Answers
Detect mouse inputs when the scene is changing to another scene. 2 Answers
CATCH MOUSE CLICK EVENT ON OBJECTS 0 Answers
Mouse Clicks Stop After X# of clicks 0 Answers
Event on mouse up 2 Answers