- Home /
Question by
IckytheEskimo · Jan 25, 2020 at 10:14 PM ·
animationfade
How do I play an animation, wait, then fade it out?
Basically what the title asks. I want to play my animation, then after the animation is finished playing, I want there to be a small delay before it fades out of the scene in about 2 seconds. After it fades out, it should be disabled and reset.
Here's my code so far, but when I set unlocking to true, it does nothing.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class AnimationController : MonoBehaviour
{
public Animator anim;
public SpriteRenderer sr;
public bool unlocking;
Sprite unlockSprite;
Sprite lockSprite;
Color temp;
// Start is called before the first frame update
void Start()
{
unlocking = false;
unlockSprite = Resources.Load<Sprite>("unlock");
lockSprite = Resources.Load<Sprite>("lock");
}
// Update is called once per frame
void Update()
{
if (unlocking) {
anim.Play("unlock");
unlocking = false;
sr.sprite = unlockSprite;
System.Threading.Thread.Sleep(1000);
StartCoroutine(FadeTo(0.0f, 2.0f));
temp = new Color(sr.color.r, sr.color.g, sr.color.b, 0f);
sr.color = temp;
gameObject.SetActive(false);
sr.sprite = lockSprite;
temp = new Color(sr.color.r, sr.color.g, sr.color.b, 1f);
sr.color = temp;
}
}
IEnumerator FadeTo(float aValue, float aTime)
{
float alpha = sr.color.a;
for (float t = 0.0f; t < 1.0f; t += Time.deltaTime / aTime)
{
Color newColor = new Color(sr.color.r, sr.color.g, sr.color.b, Mathf.Lerp(alpha, aValue, t));
sr.color = newColor;
yield return null;
}
}
}
Comment
Your answer
Follow this Question
Related Questions
Fade animation in 1 Answer
scale animation result? 1 Answer
Who Killed Who - Scrolling GUI Text 2 Answers
Fade audio in and out for Jet Engine 1 Answer
Advanced fading effect 1 Answer