- Home /
Make Animation Loop X Times C#
I am currently using this method with a UI button to play and stop my animation on a GUI element. It works but not quite exactly what I want. I would like to try and make it so that the animation plays "X" amount of times when the button is pressed. I think a Trigger is what I need? This is how I'm currently doing it with a Bool.
public void PlayAnalyzerLightsAnimation()
{
Analyzerlights.SetBool("AnalyzerLightsOff", !Analyzerlights.GetBool("AnalyzerLightsOff") );
}
Not being critical but use the code button when you put code, makes it more readable and, therefore, more likely to get an answer. It's the
101 010
button. Don't worry I've done for you this time.
I'm not sure if this will help but if you know how long the animation takes could you set a float to Time.time + (AnimLength * 10) and to follow my own example:
private float AnimLength = 1.2f;
private float PlayTen = 0f;
public void PlayAnalyzerLightsAnimation()
{
bool tempBool = Analyzerlights.GetBool("AnalyzerLightsOff");
Analyzerlights.SetBool("AnalyzerLightsOff", !tempBool);
if(!tempBool)
PlayTen = Time.time + (AnimLength * 10);
}
void Update()
{
if(Time.time >= PlayTen && PlayTen != 0f)
{
PlayerAnalyzerLightsAnimation();
PlayTen = 0f;
}
Although not tested that code.
It gives me the following error message: Assets/Scripts/Analyzer$$anonymous$$anagerScript.cs(127,33): error CS0103: The name `PlayerAnalyzerLightsAnimation' does not exist in the current context
Never $$anonymous$$d I figured out what it was it was supposed to say Play ins$$anonymous$$d of player ;)
Good and don't forget that 1.2f is just an example. You'll need to replace it with the time your animation takes to play.
1 typo in untested code is pretty good considering how bad my typing is, sorry I'd normally test code before posting but just went in to fix your code layout and got carried away!
Answer by Mmmpies · Feb 02, 2015 at 08:14 PM
Looks like a typo, I only went in to change you code section and got carried away try this:
private float AnimLength = 1.2f;
private float PlayTen = 0f;
public void PlayAnalyzerLightsAnimation()
{
bool tempBool = Analyzerlights.GetBool("AnalyzerLightsOff");
Analyzerlights.SetBool("AnalyzerLightsOff", !tempBool);
if(!tempBool)
PlayTen = Time.time + (AnimLength * 10);
}
void Update()
{
if(Time.time >= PlayTen && PlayTen != 0f)
{
PlayAnalyzerLightsAnimation();
PlayTen = 0f;
}
Pluralized the PlayAnalyzerAnimation(); line
Thanks $$anonymous$$mmpies, I had already made the fix but this works ;)
Answer by osadst · Jun 03, 2017 at 11:32 AM
Hi guys, I am trying to modify sthe olution above to be able to play my animation clip X times after pressing the button and going back to Idle animation after that. I am graphic designer, not a programmer and need to make it for my work placement. I'd highly appreciate any help.
cheers guys!