- Home /
How to randomly start & stop an animation?
I'm fairly new to Unity (and C#). At the moment I'm trying to make a simple 2D platformer.
I have some items in my game that the player can collect for points. I have a very simple animation for those items (it's a diamond that shines). But, I don't want to have all the diamonds keep shining and shining. I want them to shine and then wait a random amount of time before they shine again.
I tried something with two animations (an idle state and a shining state) and I also googled a bit here and there and read about some other things. I just can't get it to work.
It's not the first time I have problems with the animator in Unity. In my experience so far I find it quite annoying. I don't know why...
Anyway, I hope someone has a simple solution for me?
Thanks!
Answer by DoTA_KAMIKADzE · Apr 10, 2015 at 02:51 PM
I guess that is what you want:
1)Add boolean parameter "Start" (you can name it as you want I'll just use that name in example) to your animator transitions. If true => set shine animation, if false => set idle animation.
2)Add this code to your diamond prefab:
public class Diamond : MonoBehaviour
{
static bool diamondsCanShine = true;
private Animator _anim;
[SerializeField]
private string startAnimation = "Start";
internal bool isAnimated
{
get { return _anim.GetBool(startAnimation); }
set { _anim.SetBool(startAnimation, value); }
}
private void Awake()
{
_anim = GetComponent<Animator>();
}
private void Start()
{
StartCoroutine(Example());
}
IEnumerator Example()
{
while (diamondsCanShine)//replace with something like "GameIsPlayedBoolean" or "LevelIsRunning" or "GameIsNotPaused" etc.
{
isAnimated = !isAnimated; //this will start and stop animation if animation never stops
//though if your animation is not loooping and stops then you can always randomize boolean istead or check if (isAnimated) or (!isAnimated) and do what you expect accordingly
yield return new WaitForSeconds(Random.Range(0f, 10f));//this will randomly wait next time the process changes, set min and max wait time
}
}
}
Thanks a lot!
$$anonymous$$y own code was quite close to yours, but it looks like you fixed some things I wasn't able to get working.
However, I can't test it, because, well, I don't completely understand how to accomplish step 1. I added a boolean parameter called "Start" into the animator, but how do I make a transition using that boolean?
Ah, I see. I was able to make a transition with the parameter to the shine state. But, I can't edit (add the parameter) on the transition to the default state (the orange block). I can only add a 2nd transition to the default state.
I hope that's understandable...
Anyway, I appreciate your help, but I also understand if... well, if you start to find my questions annoying ;)
You can edit transitions - click with left mouse(L$$anonymous$$B) on the transition "arrow" in order to edit transition.
P.S. Just in advance - to delete transition select it and press Delete button.
Transition from entry just defines the first state node, you can change it by clicking with right mouse (R$$anonymous$$B) on any other transition and setting it as "Default State".
As for the last - check the exit time, transition duration and offset. That is all configurable under transition settings.
Your answer
Follow this Question
Related Questions
2D Animation does not start 1 Answer
Damn, HOW CAN I KNOW, IF MY 2d ANIMATION SPRITE IS FINISHED??!! 0 Answers
Best way to do animate soldier? 1 Answer
Animation transform lock 0 Answers
How can i play a animation? 0 Answers