- Home /
Only execute function if mouse has hovered object for more then X time
I have a simple object with an info area. When the mouse moves over that area an InfoBox is being instantiated for a few seconds. Right now the box is instantiated whenever my mouse touches the area. I want to make a condition for the mouse to touch the area for at least x time before the condition is now true and the InfoBox can be instantiated.
Answer by simonvdm132 · Jul 10, 2020 at 09:41 AM
Maybe try this:
bool isHovering = false;
public float timeToWait = 0.5f;
float timeLeft;
void OnMouseOver()
{
timeLeft = timeToWait;
isHovering = true;
}
void OnMouseExit()
{
isHovering = false;
}
void Update()
{
if(isHovering)
{
timeLeft-=Time.deltaTime;
}
if(timeLeft <= 0)
{
// Show Box
}
}
Note that this should be on the gameobject your mouse should hover above. Link
Answer by Artik2442 · Jul 10, 2020 at 09:46 AM
Maybe you can use a Coroutine, Like that:
void Update()
{
if(/*mouse position is in the area*/)
{
StartCoroutine(Verify());
}
}
IEnumerator Verify()
{
yield return new WaitForSeconds (5f); // Wait 5 seconds
if (/*mouse position is in the area*/)
{
//Instantiate the box
}
}
Didn't test it, so I'm not 100% sure but test it!
Your answer
Follow this Question
Related Questions
GUI.Button on MouseHover 1 Answer
Mouse Glitching? 0 Answers
GUI texture to change texture when hovered over or clicked? 2 Answers
Action on mouse button up 1 Answer
Destroy A Game Object After Mouse is held for a certain amount of time 1 Answer