- Home /
Low FPS because of changing sprites
I am making a top down 2D zombie shooter game. Much like the SAS games, there are barricades in the game. The barricades consists of 6 sprites. The sprite that is shown depends on the amount of hitpoints that the barricade has. If the barricade has 90% hit points left, the sprite is changed, if it has 75% left the sprite is changed and so on until the barricade is broken.
The problem is that I get around 21 FPS when there are 4 barricades being damaged by zombies. As soon as the barricades have either broken or the zombies are killed the FPS goes back to about 200. Why so? Is this a bad method for sprite rendering? This is one of my first games, keep that in mind! This is the code:
if (gameObject.GetComponent<HealthController> ().currentHealth >= maxHp) {
gameObject.GetComponent<SpriteRenderer>().sprite = undamaged;
}
if (gameObject.GetComponent<HealthController> ().currentHealth < maxHp * 0.99f && gameObject.GetComponent<HealthController> ().currentHealth > maxHp * 0.75f) {
gameObject.GetComponent<SpriteRenderer>().sprite = damaged1;
}
if (gameObject.GetComponent<HealthController> ().currentHealth < maxHp * 0.75f && gameObject.GetComponent<HealthController> ().currentHealth > maxHp * 0.6f) {
gameObject.GetComponent<SpriteRenderer>().sprite = damaged2;
}
if (gameObject.GetComponent<HealthController> ().currentHealth < maxHp * 0.6f && gameObject.GetComponent<HealthController> ().currentHealth > maxHp * 0.45f) {
gameObject.GetComponent<SpriteRenderer>().sprite = damaged3;
}
if (gameObject.GetComponent<HealthController> ().currentHealth < maxHp * 0.45f && gameObject.GetComponent<HealthController> ().currentHealth > maxHp * 0.3f) {
gameObject.GetComponent<SpriteRenderer>().sprite = damaged4;
}
if (gameObject.GetComponent<HealthController> ().currentHealth < maxHp * 0.3f && gameObject.GetComponent<HealthController> ().currentHealth > maxHp * 0.15f) {
gameObject.GetComponent<SpriteRenderer>().sprite = damaged5;
}
Appreciate some answers!
Answer by Sealmeister · Aug 10, 2015 at 12:33 PM
Solved it guys! I was looking in the profiler and noticed that GUI.Repaint was at 97%! In my healthcontroller, I render healthbars. The healthbars were rendered for barricades too. I fixed this by adding:
if (gameObject.tag == "Player" || gameObject.tag == "Zombie"){ renderHealthbars }
Now I get 300 FPS:) Thanks a bunch!
Answer by tanoshimi · Aug 10, 2015 at 11:45 AM
You appear to be calling gameObject.GetComponent<HealthController>()
11 times every frame. Not only does this make your code very hard to read, it's going to be slow. Access it once and save it in a variable. And, seeing as your if
conditions are mutually-exclusive, use else if
to prevent unnecessary tests.
Something more like this:
private HealthController hC;
private SpriteRenderer sR;
public void Start() {
// Cache the components *once* in Start()
hC = gameObject.GetComponent<HealthController>();
sR = GetComponent<SpriteRenderer>();
}
void Update() {
if(hC.currentHealth >= maxHp) {
sR.sprite = undamaged;
}
else if (hC.currentHealth > maxHp * 0.75f) {
sR.sprite = damaged1;
}
else if (hC.currentHealth > maxHp * 0.6f) {
sR.sprite = damaged2;
}
else if (hC.currentHealth > maxHp * 0.45f) {
sR.sprite = damaged3;
}
... etc. etc....
I will try this! What happens if I only write "SpriteRenderer sR;" ins$$anonymous$$d of "private SpriteRenderer sR;"?
EDIT:
if (hC.currentHealth >= maxHp) {
sR.sprite = undamaged;
}
else if (hC.currentHealth < maxHp * 0.99f && hC.currentHealth > maxHp * 0.75f) {
sR.sprite = damaged1;
}
else if (hC.currentHealth < maxHp * 0.75f && hC.currentHealth > maxHp * 0.6f) {
sR.sprite = damaged2;
}
else if (hC.currentHealth < maxHp * 0.6f && hC.currentHealth > maxHp * 0.45f) {
sR.sprite = damaged3;
}
else if (hC.currentHealth < maxHp * 0.45f && hC.currentHealth > maxHp * 0.3f) {
sR.sprite = damaged4;
}
else if (hC.currentHealth < maxHp * 0.3f && hC.currentHealth > maxHp * 0.15f) {
sR.sprite = damaged5;
}
I still get 21 FPS though :/
And when you look at the Profiler, what's using up all the resources?
I would also run that code only when you update your barricade's health rater than for each update. You wont gain much (just a few comparaisons) but your code will be clearer and easier to maintain.
The fact that FPS goes back to 200 after zombies are killed is interesting. Something else might be going on at the zombie level. Depends a lot on your code.
Your answer
Follow this Question
Related Questions
Arrays? ↓↓↓↓↓↓↓ 2 Answers
Sprite not being rendered on play mode but it appears on the editor. 1 Answer
Why are my sprites wierd in Unity? 0 Answers
Creature Editor - Sprite Cel Shading/ Dynamic Hard Shadows 1 Answer
How can I enlarge the sprite to avoid the movement of the character when the pivot change? 0 Answers