1
\$\begingroup\$

This is script is attached to a black plane. I have a plane and i added to it a material and the plane is in black. When running the game the alpha color of the plane material is changing slowly.

The script is working it's getting slowly from dark black to none black. But i want to make some changes:

It's not my script i took it from the youtube and did some changes the first one converted from js to c# second added the fade bool variable.

First of all i don't see any meaning setting the variable timer to 10 at the top. Any ideas what is the point ? Second is how can i check when the alpha color value is high enough to do some stuff:

  1. Enabled true the fpc again. Once it's not black again the fpc is still enabled false.

  2. If i want when the alpha color value reached it's max to make automatic fade back to black Now i'm using manual fade bool variable but i wonder how to do it automatic fade in/out.

    using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityStandardAssets.Characters.FirstPerson;

    public class FadeScript : MonoBehaviour { public float timer = 10.0f; public FirstPersonController fpc; public bool fade = true;

    // Use this for initialization
    void Start()
    {
        Color tempcolor = GetComponent<Renderer>().material.color;
        tempcolor.a = 1f;
        GetComponent<Renderer>().material.color = tempcolor;
    
        fpc.enabled = false;
    }
    
    // Update is called once per frame
    void Update()
    {
        timer = Time.deltaTime;
    
        if (timer >= 0)
        {
            Color tempcolor = GetComponent<Renderer>().material.color;
            if (fade == true)
            {
                tempcolor.a -= 0.1f * Time.deltaTime;
            }
            else
            {
                tempcolor.a += 0.1f * Time.deltaTime;
            }
            GetComponent<Renderer>().material.color = tempcolor;
        }
    }
    

    }

\$\endgroup\$

1 Answer 1

2
\$\begingroup\$

Here is how I would have done it (I've reworked your script, tell me if you don't understand something)

using UnityEngine;
using UnityStandardAssets.Characters.FirstPerson;

public class FadeScript : MonoBehaviour
{
    public FirstPersonController fpc;
    private Material material ;
    private float targetAlpha = 1 ;
    private float fadeDuration = 1 ;
    private float lerpParam ;
    private float startAlpha = 0 ;

    void Start()
    {
        // It's nice to cache the material to quickly and easily access it
        material = GetComponent<Renderer>().material ;
        SetMaterialAlpha( 1 ) ;

        fpc.enabled = false;
    }

    void Update()
    {
        lerpParam += Time.deltaTime ;

        // Lerp let's you change slowly the value of the alpha
        // https://docs.unity3d.com/ScriptReference/Mathf.Lerp.html
        float alpha = Mathf.Lerp( startAlpha, targetAlpha, lerpParam / fadeDuration ) ;
        SetMaterialAlpha( alpha  ) ;
        if( material.color.a >= 1 )
        {
            FadeTo( 0 );
        }
    }

    // Function to start the fade with the specific alpha with the desired duration
    public void FadeTo( float alpha, float duration = 1 )
    {
        startAlpha   = material.color.a ;
        targetAlpha  = alpha ;
        fadeDuration = duration ;
        lerpParam    = 0 ;
    }

    // Easily change the material alpha with a specific function
    private void SetMaterialAlpha( float alpha )
    {
        Color color = material.color;
        color.a = alpha;
        material.color = color;
    }
}

In your script, setting the timer value to 10 is useless since you "override" this value each frame timer = Time.deltaTime (the timer variable is not used, so I don't understand what you were trying to achieve).

About your two other questions, I didn't understand everything and I hope the script I gave you will help you to answer those questions by yourself.

\$\endgroup\$
0

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .