- Home /
Frame Rate gradually decreasing when Spotlight is adjusted ingame
Good day. I implemented a script to gradually decrease the light source of my character in game. This serves as the game's timer. I use a Spot Light as the light source and adjust its spot angle proportional to the elapsed time. However, I observed that the frame rate drops gradually as the light depletes. I'm wondering what's causing this behavior or is it the light that really affect the game's frame rate. Below is my code for the Spot Light adjustment.
private void UpdateLight() {
this.timer.Update();
float timePercentage = (this.duration - this.timer.TimeElapsed()) / this.duration;
float newAngle = this.light.spotAngle;
if(timePercentage > 0.75f) {
newAngle = this.lightPower;
}
else if(timePercentage > 0.50f) {
newAngle = (0.9f * this.angleRange) + Constants.MIN_LIGHT_ANGLE;
}
else if(timePercentage > 0.25f) {
newAngle = (0.75f * this.angleRange) + Constants.MIN_LIGHT_ANGLE;
}
else if(timePercentage > 0.1f) {
newAngle = (0.25f * this.angleRange) + Constants.MIN_LIGHT_ANGLE;
}
else if(timePercentage > 0.0f) {
newAngle = Constants.MIN_LIGHT_ANGLE;
}
if(this.timer.HasElapsed()) {
RenderSettings.ambientLight = Color.black;
Destroy(this.gameObject);
}
if(newAngle != this.light.spotAngle) {
this.light.spotAngle = newAngle;
GameManager.Instance.StrataHud.UpdateLightBar(timePercentage);
}
}
There's a flag for my light source which tells if the light is permanent or depleting. The frame rate drop happens when the light is depleting which is the condition for UpdateLight function to trigger.
The UpdateLight function is invoked in the Update routine of the object. If it will help, my scene is made up of tk2d sprites with lit shaders. It's tile based and so far I'm playing on a 18 x 35 tile scene with each tile measuring 256px by 256px. I'm using the free version of Unity.
If it helps, here's my list of the running processes. Not sure if I can enumerate them all but this is what I've reviewed so far:
character movements (current action of character and monsters)
HUD processes (progress bars, label changes, notification handling made up of text)
Hint detection (triggered periodically by specific type of objects in order to trigger the hint arrow. This is done by measuring the vector distance from the character to the object. Each hintable object has this process)
Do you have a redundant call in either the "this.timer.Update()" or the "Game$$anonymous$$anager.Instance.StrataHud.UpdateLightBar()"?
The light shouldn't cause a lag over time.
Do you instantiate something in each call? Is this the only script you are running? Do you have any other methods with redundant calls?
There are many possible reasons for a gradualy decrase of the performance. Please check the possibilities I've listed, else we would need more information. In your script. I don't see anything that should decrase performance.
Greetings
Chillersanim
Thanks for the response. this.timer is just my custom class for a countdown timer which uses Unity's Time class for computation and each instance is only called once per frame per $$anonymous$$onoBehavior since the only practical way to update it is through the Update function. In turn, the UpdateLight function is called by only one class so no redundant calls involved. As for instantiation, I don't instantiate anything unless it is triggered by an action. There's a flag for my light source which tells if the light is permanent or depleting. The frame rate drop happens when the light is depleting which is the condition for UpdateLight function to trigger.
Is the framerate drop persistent? Or it is just a short framedrop and normalises afterwards?
I found nothing about performance isues with dark scenes, perhaps you should test your script in a new scene with only this setup (no other logic or models, perhaps a cube). If the probles is still the same, then I would ask you to post also the UpdateLight function, so that I can look if there's a performance critical part.
Greetings Chillersanim