- Home /
Triggers causes scene to lag
Hi, My name is Dave. I have been working on a Unity based Horror for quite sometime now and all is well so far i have received good feedback however recently decided that some room doors would be shut and you would walk into them (they have trigger colliders) and my script that's attached to each door would play the audio (door opening sound), fade out the current scene and load the Scene declared in the scripts variable. And then ultimately fade in the next scene and so on you get the idea. Many doors have these triggers placed closely on front of them. but since i have put them there my scene lags alot. Even with occlusion culling throughout it. and static batching. I have tested this theory by simply disabling all triggers and then playing. The triggers go, the lag goes.... simple....
here's a copy of my script:
public var fadeOutTexture : Texture2D;
public var fadeSpeed = 2.0;
var drawDepth = -1000;
var alphaWait : boolean = true;
public var levelName ="SceneName";
var delayTime : float = 1.5;
private var alpha = 1.0;
private var fadeDir = -1;
function OnTriggerEnter(other : Collider)
{
audio.Play();
fadeOut();
Application.LoadLevel(levelName);
}
function OnGUI(){
if(alphaWait == false) {
alpha += fadeDir * fadeSpeed * Time.deltaTime;
}
alpha = Mathf.Clamp01(alpha);
GUI.color.a = alpha;
GUI.depth = drawDepth;
GUI.DrawTexture(Rect(0, 0, Screen.width, Screen.height), fadeOutTexture);
}
//--------------------------------------------------------------------
function fadeIn(){
yield WaitForSeconds(2);
alphaWait = false;
fadeDir = -1;
}
//--------------------------------------------------------------------
function fadeOut(){
fadeDir = 1;
}
function Start(){
alpha=1;
fadeIn();
}
Are you sure it's the trigger and not the OnGUI code? (somewhat notorious for being slow)
shoot! never consider that. I did have issues with OnGUI before but it never raised a flag with me..... good insight my friend thank you. So what you think i should do to rectify the problem because i have searched high and low for alternatives to OnGUI calls...
Answer by YandereMobile · Feb 12, 2020 at 07:01 AM
Yeah duh. They're really lag. Try finding ways to not use them. For example, if your making like a stealth-game use a navmesh agent. Or Invoke the trigger;
Please do not post answers like this on questions which date back 6 years except if your answer actually brings up something new which is related to a new version of unity for example and makes other old answers obsolete...
Answer by j4ke · Feb 12, 2020 at 10:16 AM
Not sure, but it might be that OnGUI causes the lagging since OnGUI causes performance costs at my knowledge. How about making the screen fade effect by using UI canvas and coroutines?
Your answer
Follow this Question
Related Questions
Laggy 3D game on Android 0 Answers
Trigger doesn't work 100% of the time. 1 Answer
Does OnTriggerStay don't detect a collision with a non-trigger collider? 1 Answer
Problems with the Trigger Collider (randomly fictional) 1 Answer
Where/ How can I find out what is affecting my webplayer window-mode performance? 0 Answers