- Home /
Activating only when mouse is held down on a button for 0.5s.
Hi guys! C# noob here, but doing my very best with what I know so far. I need to have a collider activating only when it is held down for 0.5 seconds. Is it possible for it to be done via a Pointer Down Event Trigger? Also, I'm trying to use the Event Trigger mainly because I have a lot of collider buttons that in the game and I would like to use this to easily allocate functions.
To provide some context, I have a separate script where an UI image will appear and do a radial fill (to look like its searching/loading) and when the button is held for the allocated time(0.5s) the desired action would happen("You found an item in the bush!"). And if you release the button early nothing will happen.
Essentially similar to the Gwent single player game, Thronebreaker's looting mechanic.
Here's what I tried to do but it doesn't seem to be working :/ The idea is to have a coroutine checking on conditions when the button is held down. And stops checking if button is released. Any help is appreciated! thanks in advance!
public void OnButtonClick1()
{
StartCoroutine(Checker1());
}
IEnumerator Checker1()
{
while(true)
{
if(Fillamt > 0.8f)
{
//"You found an item in the bush!"
break;
}
if (Input.GetMouseButtonUp(0))
{
break;
}
else
{
yield return null;
}
}
}
Answer by yongslyy · Jul 23, 2020 at 06:50 AM
Oh nvm I figured it out. The code above works just fine. What I did wrong was assigning the Fillamt in the Start() function and it froze the value on game start.
I added the checking of Fillamt in the Checker1() coroutine and it worked.
Your answer
Follow this Question
Related Questions
onMouseDown call function from other script 1 Answer
Picking up an object to allow power-up, using external script 1 Answer
Call OnMouseDown of collider? 0 Answers
Custom UI element click/touch area 0 Answers
Hard time getting mouse click to work 2 Answers