- Home /
this animation is too fast. please help
Hi! when I add this script to my 2d character, the animations is just way to fast, and I can't control it.
var FPS: float;
var frames : Texture[];
var running : boolean;
private var secondsToWait : float;
private var currentFrame: float;
function Start ()
{
currentFrame = 0;
secondsToWait = 1/FPS;
AnimateRun ();
}
function AnimateRun ()
{
if(currentFrame >= frames.Length)
{
currentFrame = 0;
}
yield WaitForSeconds (secondsToWait);
renderer.material.mainTexture = frames[currentFrame];
currentFrame++;
}
function Update ()
{
if(running == true)
{
AnimateRun ();
}
}
I think that it's a simple script and all and it should work. but it doesn't. Is there some way to make it go slower? thanks
The type of currentFrame
is surprising. It should be int
.
Answer by Kryptos · Jul 18, 2012 at 11:16 AM
The problem is that you call a new instance of the coroutine AnimateRun
each frame inside the Update
function. This is wrong.
You should do something like that (sorry the code is in C#, because I never use UnityScript):
public float FPS;
public Texture[] frames;
public bool isRunning;
public float secondsToWait;
public int currentFrame;
IEnumerator Start()
{
currentFrame = 0;
secondsToWait = 1.0f/FPS;
while(true)
{
if(isRunning)
{
yield return StartCoroutine(AnimateRun());
}
else
{
yield return null;
}
}
}
IEnumerator AnimateRun()
{
if(currentFrame >= frames.Length)
{
currentFrame = 0;
}
yield return new WaitForSeconds (secondsToWait);
renderer.material.mainTexture = frames[currentFrame];
currentFrame++;
}
Alternatively, you could check the isRunning
variable inside the AnimateRun
coroutine. This will ensure that your animation keep in sync.
IEnumerator Start()
{
currentFrame = 0;
secondsToWait = 1.0f/FPS;
StartCoroutine(AnimateRun());
}
IEnumerator AnimateRun()
{
while(true)
{
yield return new WaitForSeconds (secondsToWait);
if(isRunning)
{
renderer.material.mainTexture = frames[currentFrame%frames.Length];
currentFrame++;
}
}
}
Answer by frankyboy450 · Jul 18, 2012 at 11:29 AM
I followed a tutorial on how to animate in 2d. but it was also in C# this script below works fine but my character script is written in UnityScript.
using UnityEngine;
using System.Collections;
public class Animation : MonoBehaviour {
public float FPS;
private float secondsToWait;
public bool Loop;
public Texture[] frames;
private int currentFrame;
// Use this for initialization
void Start ()
{
currentFrame = 0;
secondsToWait = 1/FPS;
StartCoroutine(Animate());
}
IEnumerator Animate()
{
bool stop = false;
if(currentFrame >= frames.Length)
{
if(Loop == false)
stop = true;
else
currentFrame = 0;
}
yield return new WaitForSeconds(secondsToWait);
renderer.material.mainTexture = frames[currentFrame];
currentFrame++;
if(stop == false)
StartCoroutine(Animate());
}
}
I managed to translate it into unityscript but it's not working though. "The type of currentFrame is surprising. It should be int." I searched the unity references site but couldn't find anything about "int" what does it do?
Don't post comment as answer.
int
, like float
is a type of variable. While float
allows digits, int
as its name suggests is for integer values.
Your translation was not correct. There was no Update
method in the original script. Also the variable currentFrame
had the correct type int
.
But consider using my version of the script. If find the use of the internal bool inside the coroutine (and the interleaved start of a new coroutine at the end) a bit awkward.
Ok. won't do that from now on.
thanks for explaining ;) I'll try to figure out why this script I have doesn't work
hmm... still not working. unity just shows me alot of errors, Can't see what I'm doing wrong. I wanted to try and call the AnimateRun function in the end of the AnimateRun function so that it would just loop. but it just shows errors to me ;S
ok. I tried using the "InvokeRepeating" code. and now it works :D Thanks for helping me $$anonymous$$ryptos. I'm very grateful
Your answer

Follow this Question
Related Questions
2D Animation does not start 1 Answer
Zombie Cafe style game. 1 Answer
Make 2d Animation Scene 0 Answers
Making transform based animation "snap" when mixing with sprite based animation 2 Answers
2D sprite character movement 3 Answers