I want to show/hide one of the enemies in my intro scene by LERPing a guiTexture (positioned in front of the enemy) from black to clear and clear to black. This is my script:
using UnityEngine;
using System.Collections;
public class ScreenFadeInOut_scr : MonoBehaviour {
private float fadeStart;
private bool isFading = false;
public float fadeDuration;
private bool enemyVisible = false;
void Awake()
{
guiTexture.pixelInset = new Rect(0, Screen.height - Screen.height / 2.3f, Screen.width / 4.4f, Screen.height / 2.3f);
fadeDuration = 10f;
}
void Start()
{
}
void Update()
{
if (!enemyVisible && isFading)
{
ShowEnemy();
}
else if (enemyVisible && isFading)
{
HideEnemy();
}
}
void ShowEnemy()
{
float timeSinceStarted = Time.time - fadeStart;
float percentageComplete = timeSinceStarted / fadeDuration;
guiTexture.color = Color.Lerp(guiTexture.color, Color.clear, percentageComplete);
Debug.Log("percentageComplete = " + percentageComplete);
if(percentageComplete >= 1f)
{
isFading = false;
enemyVisible = true;
Debug.Log("enemyVisible = " + enemyVisible);
guiTexture.enabled = false;
}
}
void HideEnemy()
{
guiTexture.enabled = true;
float timeSinceStarted = Time.time - fadeStart;
float percentageComplete = timeSinceStarted / fadeDuration;
guiTexture.color = Color.Lerp(guiTexture.color, Color.black, percentageComplete);
Debug.Log("percentageComplete = " + percentageComplete);
if (percentageComplete >= 1f)
{
isFading = false;
enemyVisible = false;
Debug.Log("enemyVisible = " + enemyVisible);
}
}
void OnGUI()
{
if (GUI.Button(new Rect(Screen.width / 4, Screen.height / 3, 50, 50), "S/H"))
{
fadeStart = Time.time;
isFading = true;
}
}
}
When I click the Show/Hide button the percentageComplete variable goes all the way (full 10 seconds), but the enemy becomes visible/invisible in just a few seconds (1.5 to 3). How do I fix this?