How can i check if the alpha color or the material color is transparent ?
I have a Plane in the Hierarchy the Plane is black and i added a material to the Plane and in the Material after changed the Material to black i also changed it to Sprites > Diffuse
The script is working as it is now.
It will change from black to transparent like awaking effect. And if i change the fade bool state while the game is running it will change the fading to black or from black.
But now i need to do more two things and not sure how to:
1 Make that when the plane or maybe it's the material on the plane i'm not sure is transparent not black any more then do something for example make fpc.enabled = true; again.
2 Make the fade in out automatic now i'm using the bool fade and i change the state on my own but i want to do that once the plane or material is transparent make it back slowly black again and so on none stop automatic fade in/out.
Not sure if i should use startcoroutine for this or not and how to do it. For example now after the player is awake the fpc is still enabled false the idea the logic is that once the awake effect the fade effect finish the player can move fpc.enabled = true; but i don't know when the fading effect has finished reached to the end.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityStandardAssets.Characters.FirstPerson;
public class FadeScript : MonoBehaviour
{
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()
{
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;
}
}
Answer by pako · Nov 05, 2017 at 09:48 AM
@Chocolade add 2 more bool variables: isBlack and isTransparent, and then change your existing Update with the one below:
bool isBlack
bool isTransparent
void Update()
{
Color tempcolor = GetComponent<Renderer>().material.color;
isBlack = Mathf.Approximately(tempcolor, 1f);
isTransparent = Mathf.Approximately(tempcolor, 0f);
if(isBlack)
{
fade = true;
}
else if(isTransparent)
{
fade = false;
//do something for example:
fpc.enabled = true;
}
if (fade == true)
{
tempcolor.a -= 0.1f * Time.deltaTime;
}
else
{
tempcolor.a += 0.1f * Time.deltaTime;
}
GetComponent<Renderer>().material.color = tempcolor;
}
I haven't tested this, but it should work. Let me know if there's a problem with it.
Your answer
Follow this Question
Related Questions
How can i disable/enable slowly the blur effect using the blur script ? 0 Answers
How can I give each instance a enum mode from another script ? 0 Answers
How can i make the right choice of implementation of my shooting fire script ? 1 Answer
How can i set the camera to be automatic behind the player ? 0 Answers
Saving Data (please help) 0 Answers