I’m using this script to fade two groups of object. Script automatically pick all child of two object and fade between them. All object have material with alpha channel.
Script working, but is not fluid. I’m a novice programmer. Can anyone tell me if I’m doing something wrong by accident?
Thanks.
#pragma strict
var OBJFirst : GameObject;
var OBJSecond : GameObject;
private var OBJFirstArray : Component[];
private var OBJSecondArray : Component[];
var AlphaStartFirst : float = 0.0;
var AlphaStartSecond : float = 0.0;
var SingleTouch : boolean = true;
var DoubleTap : boolean = false;
var Swipe : boolean = false;
var AlphaHigh : float = 1.0;
var AlphaLow : float = 0.0;
private var alphaFirst : float ;
private var alphaSecond : float ;
var velocity : float = 1.0;
var TimeDelay : float = 0.0;
private var time : float;
private var var_Fade : boolean;
private var GoTouch : boolean = true;
function Awake () {
GetComp();
alphaFirst = AlphaStartFirst;
alphaSecond = AlphaStartSecond;
}
function Start () {
}
function Update () {
if (var_Fade){
fun_Fade();
}
}
function fun_Fade(){
if( alphaFirst > 0){
for (var child1 : Component in OBJFirstArray) {
child1.renderer.material.color.a = alphaFirst;
for (var child2 : Component in OBJSecondArray) {
child2.renderer.material.color.a = alphaSecond;
}
time = time + Time.deltaTime*1/velocity;
alphaFirst = Mathf.Lerp(AlphaHigh,AlphaLow,time);
alphaSecond = Mathf.Lerp(AlphaLow,AlphaHigh,time);
}
else if (alphaFirst == 0){
var_Fade = false;
OBJFirst.SetActiveRecursively(false);
Manager.GoTouch = true;
}
}
function GetComp(){
OBJFirstArray = OBJFirst.GetComponentsInChildren(Component);
OBJSecondArray = OBJSecond.GetComponentsInChildren(Component);
}
function On_TouchStart(gesture:Gesture){
if (Manager.GoTouch){
if (SingleTouch){
Manager.GoTouch = false;
alphaFirst = AlphaHigh;
alphaSecond = AlphaLow;
OBJSecond.SetActiveRecursively(true);
yield WaitForSeconds (TimeDelay);
var_Fade = true;
}
}
}