- Home /
Strange animation behaviour trying to implement hover functionality
Having a simple grid of big white squares (image sprites) with a starting opacity of zero i want to implement so that they fade in / out on mouse enter / exit. Every Square has a Collider (is trigger) attached.
Everything works fine, but sometimes when hovering above the screen quite fast some squares begin to "hang" in a mode where they don't fade out anymore. Instead the following happens:
The animator has 2 Triggers ("FadeIn" and "FadeOut") and when hovering the "broken" square the Animator's "FadeIn" Trigger just stays On:
The script attached looks as follow:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class GridTest : MonoBehaviour
{
Animator animator;
void Awake()
{
animator = GetComponent<Animator>();
}
void OnMouseEnter()
{
print("OnMouseEnter");
animator.SetTrigger("FadeIn");
}
void OnMouseExit()
{
print("OnMouseExit");
animator.SetTrigger("FadeOut");
}
}
The print statements for "OnMouseEnter" and "OnMouseExit" are triggerd correctly, though.
This also seems to be only happening when using 4 or more squares.
Thanks in advance for any help
Answer by ryanmillerca · Jul 18, 2019 at 09:07 PM
I suspect your triggers are getting "stuck" from being triggered too fast. Try manually resetting them before calling them.
void OnMouseEnter()
{
print("OnMouseEnter");
ResetTriggers();
animator.SetTrigger("FadeIn");
}
void OnMouseExit()
{
print("OnMouseExit");
ResetTriggers();
animator.SetTrigger("FadeOut");
}
void ResetTriggers()
{
animator.ResetTrigger("FadeIn");
animator.ResetTrigger("FadeOut");
}
As I could not reproduce the behaviour with your adjustments it seems that did the trick. Thank you!