- Home /
Trouble with OnGUI and Update
I am trying to make a GUI Texture fade in as the game starts. Currently the image does not fade and just hangs at zero oppacity. I am using OnGUI to draw the texture and Update to fade the textures alpha value.
var guiImage : Texture2D;
var guiPosition : Rect;
var guiColor = Color.white;
var scaleType : ScaleMode;
var horizRatio : float = 800;
var vertRatio : float = 600;
var depthValue : float = 1;
function Update(){
guiColor.a = Mathf.Lerp(0,1,Time.deltaTime);
}
function OnGUI(){
scale.x = Screen.width / horizRatio;
scale.y = Screen.height / vertRatio;
scale.z = 1;
GUI.matrix = Matrix4x4.TRS (Vector3.zero, Quaternion.identity, scale);
GUI.depth = depthValue;
GUI.color = guiColor;
GUI.DrawTexture(guiPosition,guiImage,scaleType,true);
}
Answer by whydoidoit · Jan 04, 2013 at 01:10 PM
Your problem is that you aren't using Lerp properly - you need to either Lerp with a value between 0 and 1 (not Time.deltaTime which will always be a smallish number and hence hover around the starting value - normally you would do this by adding Time.deltaTime to a float value that starts at 0) or you can Lerp between the current value and 1 which will damp towards the target:
var t : float = 0;
...
function Update()
{
t+=Time.deltaTime;
guiColor.a = Mathf.Lerp(0,1,t);
}
Or
guiColor.a = Mathf.Lerp(guiColor.a, 1, Time.deltaTime);
I just noticed the same mistake - Thanks for pointing out though.
Answer by MarkFinn · Jan 04, 2013 at 01:08 PM
function Update(){
guiColor.a = Mathf.Lerp(0,1,Time.deltaTime);
}
is wrong.
Time.deltaTime is generally a very small number that doesn't change much from frame to frame.
What you want is to record the time you start and subtract that from current time and put that in your lerp instead (possibly multiplied by a value to modify the speed of fade-in.
Try:
var startTime=0;
function Start(){
startTime=Time.Time;
}
function Update(){
guiColor.a = Mathf.Lerp(0,1,Time.time-startTime);
}
(may not be exactly right as I don't do javascript, tweak as necessary.)
Answer by gbelini · Jan 04, 2013 at 01:06 PM
Hi,
This code work for me, this could help ?
var guiImage : Texture2D;
var guiPosition : Rect;
var guiColor : Color = Color.white;
var scaleType : ScaleMode;
var horizRatio : float = 800;
var vertRatio : float = 600;
var depthValue : float = 1;
var scale : Vector3;
var fade : boolean = true;
function Update()
{
var incr = Time.deltaTime;
if (guiColor.a - incr <= 0 )
fade = false;
else if (guiColor.a + incr >= 1)
fade = true;
if (fade)
guiColor.a -= incr;
else
guiColor.a += incr;
//guiColor.a = Mathf.Lerp(0,1, Time.deltaTime);
}
function OnGUI(){
scale.x = Screen.width / horizRatio;
scale.y = Screen.height / vertRatio;
scale.z = 1;
GUI.matrix = Matrix4x4.TRS (Vector3.zero, Quaternion.identity, scale);
GUI.depth = depthValue;
GUI.color = guiColor;
GUI.DrawTexture(guiPosition,guiImage,scaleType,true);
}
Yes for some reason that actualy works... have to take a closer look at the differences. As far as I know $$anonymous$$athf.Lerp(0,1, Time.deltaTime); should actualy work.
No, Time.delta time is the delta between this and the last frame. The value will stay about the same all the time (depends on your framerate). The t value of $$anonymous$$athf.Lerp has to go from 0 to 1
Your answer
Follow this Question
Related Questions
Lerp stopping/not working? 1 Answer
Lerping/Fading between multiple colours 1 Answer
Scene Fade in and out script not working 1 Answer