FixedUpdate logic step by step
Hi! I`m try make object animation. Animation can work only on SetActive(true) How i can change code for stop my animation without Thread.Sleep()?
C# code :
void FixedUpdate()
{
while (idleAnimation & anim_obj.activeInHierarchy)
{
anim_obj.transform.localPosition = startPos;
anim_image.color = targetColor;
// Fade 0->1
for (float t = 0; t < 1; t += Time.deltaTime / anim_Speed)
{
anim_obj.transform.localPosition = startPos;
anim_image.color = Vector4.Lerp(targetColor, startColor, t);
}
anim_image.color = startColor;
Thread.Sleep(1000);
// Move up
// Fade 1->0
for (float t = 0; t < 1; t += Time.deltaTime / anim_Speed)
{
anim_obj.transform.localPosition = Vector3.Lerp(startPos, targetPos, t);
anim_image.color = Vector4.Lerp(startColor, targetColor, t);
}
//Animation end
anim_obj.transform.localPosition = startPos;
anim_image.color = targetColor;
Thread.Sleep(500);
}
}
Answer by Harinezumi · Jan 29, 2018 at 04:25 PM
Hi there,
first a few basic ideas: FixedUpdate()
is called once every time the physics model is updated, which is at a fixed time rate, 0.02 seconds by default. FixedUpdate()
is used almost exclusively for modifying physics, e.g. adding forces, changing velocity, or moving kinematic objects.
Update()
is called on every frame rendering, depending on your FPS, at about every 0.016 seconds when running at 60 FPS, but this varies.
So your animation logic should use the fact that these functions are called repeatedly over time, which means you don't need Thread.Sleep()
, you need variables that measure time for you, and possibly use a Coroutine as well.
Also, never call Thread.Sleep()
in the Unity main thread, because the logic runs on a single thread so you would halt the whole application (this is an oversimplification, but in practice it is a useful model).
Now, to animate your object the way I think you want you would use a Coroutine the following way:
private void Start () {
StartCoroutine(Animate());
}
private IEnumerator Animate() {
anim_obj.transform.localPosition = startPos; // you only need to set the position once
float t = 0;
while (t < anim_speed) { // better way of transitioning over anim_speed seconds
anim_image.color = Color.Lerp(targetColor, startColor, t / anim_speed); // go from target color to startColor over anim_speed seconds
yield return null;
t += Time.deltaTime;
}
anim_image.color = startColor; // making sure you precisely arrive at the final color
t = 0;
while (t < anim_speed) {
anim_image.color = Color.Lerp(startColor, targetColor, t / anim_speed);
anim_obj.transform.localPosition = Vector3.Lerp(startPos, targetPos, t / anim_speed);
yield return null;
t += Time.deltaTime;
}
// making sure you precisely arrive at the target color and position
anim_image.color = targetColor;
anim_obj.transform.localPosition = targetPos;
yield return new WaitForSeconds(0.5f); // no need for this, because there are no more instructions after it, but this is the equivalent fo Thread.Sleep(500);
}
Your answer
Follow this Question
Related Questions
Get and Set only work on public variable? 1 Answer
Unity Setting Integer to 0 continuously? 0 Answers
Need help with c# 1 Answer
Unity Math is Wrong for Some Reason 0 Answers
I need help with input field ui. 0 Answers