How to program in Unity a fade transition between sprites?

Viewed 1850

I have an animation in 4 PNG images. I want to have the frames played through over the course of 1/2 second in the order 1-2-3-4-2-1 with transparency transitions.

What I wrote was supposed to have the first frame appear immediately when the parent object holding the different sprites is generated, then have it turn transparent over 1/12 of a second while the second frame turns opaque, and so forth until the last frame ends its transparent-opaque-transparent cycle.

It's probably not the most efficient way, but I made a prefab of en empty object under which are placed the 6 sprite-frames, with each sprite given an individual script.

I'm posting the first three scripts as an example:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Frame1 : MonoBehaviour
{
    private SpriteRenderer thisSprite;
    private Color alpha;
    private float timer;
    // Start is called before the first frame update
    void Start()
    {
        alpha.a = 255;
        thisSprite.GetComponent<SpriteRenderer>().color = alpha;
    }

    // Update is called once per frame
    void Update()
    {
        timer = timer + Time.deltaTime;
        alpha.a -= timer * 3060;
        thisSprite.GetComponent<SpriteRenderer>().color = alpha;
        if (timer >= 1/12)
        {
            Destroy(gameObject);
        }

    }
}

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Frame2 : MonoBehaviour
{
    private SpriteRenderer thisSprite;
    private Color alpha;
    private float timer;
    private int direction;
    // Start is called before the first frame update
    void Start()
    {
        alpha.a = 0;
        thisSprite.GetComponent<SpriteRenderer>().color = alpha;
    }

    // Update is called once per frame
    void Update()
    {
        if (direction == 0)
        {
            timer = timer + Time.deltaTime;
            alpha.a += timer * 3060;
            thisSprite.GetComponent<SpriteRenderer>().color = alpha;
            if (timer >= 1/12)
            {
                direction = 1;
            }
        }
        if (direction == 1)
        {
            timer = timer - Time.deltaTime;
            alpha.a += timer * 3060;
            thisSprite.GetComponent<SpriteRenderer>().color = alpha;
            if (timer >= 1/6)
            {
                Destroy(gameObject);
            }
        }

    }
}

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Frame3 : MonoBehaviour
{
    private SpriteRenderer thisSprite;
    private Color alpha;
    private float timer;
    private int direction;
    // Start is called before the first frame update
    void Start()
    {
        alpha.a = 0;
        thisSprite.GetComponent<SpriteRenderer>().color = alpha;
        timer -= 1 / 12;
    }

    // Update is called once per frame
    void Update()
    {
        if (direction == 0)
        {
            timer = timer + Time.deltaTime;
            alpha.a += timer * 3060;
            thisSprite.GetComponent<SpriteRenderer>().color = alpha;
            if (timer >= 1 / 12)
            {
                direction = 1;
            }
        }
        if (direction == 1)
        {
            timer = timer - Time.deltaTime;
            alpha.a += timer * 3060;
            thisSprite.GetComponent<SpriteRenderer>().color = alpha;
            if (timer >= 1 / 6)
            {
                Destroy(gameObject);
            }
        }

    }
}

They all seem to be visible the moment they are generated, and they don't fade away or even get destroyed at all. What is the issue?

Thanks.

1 Answers

As the comments suggest, using animations is a viable alternative. However, your code will not work simply because alpha accepts a value from 0 to 1 instead of 0 to 255. So simply adjust your logic to fade from 1 down to 0 and you should see your fade transitions.

Related