• 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 /
  • Help Room /
avatar image
0
Question by HarpDernier · Jul 08, 2018 at 03:21 PM · optimizationphysics2dspawningontriggerenter2dcollectible

What is the most efficient way to spawn enemies/collectibles?

My game is a fast paced 2d platformer. It takes 1-3 minutes to beat a level, they can be pretty long. Each level features a few branc$$anonymous$$ng paths with a lot of enemies and collectibles that have to be spawned in just before they get in range of the camera. I was handling spawning by having a Spawner gameobject with the spawner script and with a spawnpoint gameobject c$$anonymous$$lded to it. The spawner script would use physics2d.overlapcircle in the FixedUpdate to tell when the player was in range and then instantiate the collectible or enemy at the spawnpoint's position. The collectibles also used overlapcircle in fixedupdate to tell when contacting the player. The enemies each have their own script as well and use box/circle collider triggers to $$anonymous$$t/hurt the player but overlap circle to tell if they've been jumped on to be killed. Most levels were running fine aside from the occasional stutter, but a couple of the later levels with more complex backgrounds and a larger number of enemies and collectibles were really tanking the frame rate. I started researc$$anonymous$$ng and learning more about optimization and profiling, I figured that instantiating and destroying a large number of objects can cause a lot of slow down and that the most time taken up by the cpu was because of physics2d.fixedupdate. So I changed all my collectibles and enemies to be active or inactive rather than instatiated and destroyed and then I changed my spawner script to use ontriggerenter2d(with a big circle collider) to detect the player instead of overlapcircle. Now levels that were previously performing fine or great tank hard when there's a lot of stuff on screen. Is ontriggerenter2d really more expensive to use than overlapcircle in fixedupdate? could it have somet$$anonymous$$ng to do with all the inactive gameobjects in the $$anonymous$$erarchy? So what is the best practice here? Should I go back to overlapcircle for spawning, possibly in a coroutine rather than fixedupdate or is there a better way? Also every enemy and collectable has a script attached that deactivates it when it detects that the camera has gone passed it with an ontriggerenter2d function. Any help is much appreciated and I can provide more information if needed but t$$anonymous$$s question was already really long.

Comment
Add comment · Show 1
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 xofear · Jan 08, 2019 at 06:36 PM 0
Share

Hi there! did you ever figure this out? I'm stuck in the same boat. I use a boxCollider2d set as a trigger, when the play enters, that starts a coroutine.

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by WainWork · Jan 10, 2019 at 07:09 AM

Hi! So, if u have only one player - best practice is "drow invisible line" from every object to player, for determining collision check lenght t$$anonymous$$s line. And don't do it in update, better create custom coroutine for t$$anonymous$$s. Try learning pooling system, t$$anonymous$$s must help u too.

Comment
Add comment · Show 1 · 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 xofear · Jan 11, 2019 at 09:56 AM 0
Share

I'll try that, Thanx!!!

avatar image
0

Answer by HarpDernier · Jan 11, 2019 at 02:48 AM

@xofear @wainwork Hey guys. So what I wound up doing was moving the physics2d overlap circle from the fixed update to a coroutine that runs 10 times per second. I also made all the enemies and collectables load into the scene inactive and become active, rather than instantiated, when the overlap circle is triggered. My coroutines previously had all been returning new waitforseconds and I instead set the time to wait beforehand to cut down on cac$$anonymous$$ng. Finally, I did learn a little about object pooling and used it on some projectiles. It's running WAYYYYY better now. Thank you, I'll research the drow invisible line. Maybe I can make it even better.

Comment
Add comment · Show 2 · 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 WainWork · Jan 11, 2019 at 07:16 AM 0
Share

I'm glad it helped you. By "invisible line" I meant just a ray, from object to player (you can use subtraction between transform.position, or the ray-class itself). Trigger collisions are very heavy for the system.

avatar image xofear · Jan 11, 2019 at 09:57 AM 0
Share

This is what Im currenlty doing. Seems to work pretty ok.

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

158 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Physics2D Optimize 1 Answer

Forcibly halt movement of slow rigidbodies, but not falling ones. (2D) 1 Answer

Some question about making Android game. 0 Answers

Picking up an item and spawning a new one. 0 Answers

optimize thousands of sprite prefab 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