Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 11 Next capture
2021 2022 2023
1 capture
11 Jun 22 - 11 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Sealmeister · Aug 10, 2015 at 11:34 AM · 2d gamespritesspriterendererlow fpsframerate drops

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!

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

· Add your reply
  • Sort: 
avatar image
0
Best Answer

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!

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
1

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....
Comment
Add comment · Show 4 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Sealmeister · Aug 10, 2015 at 11:55 AM 0
Share

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 :/

avatar image tanoshimi · Aug 10, 2015 at 12:21 PM 0
Share

And when you look at the Profiler, what's using up all the resources?

avatar image Willyzekid · Aug 10, 2015 at 12:23 PM 0
Share
  • 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.

avatar image Sealmeister · Aug 10, 2015 at 12:31 PM 0
Share

Solved it! Posted the answer:) thanks guys!

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

2 People are following this question.

avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges