• 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 Ace22 · Nov 08, 2014 at 06:45 AM · frameratetopdownspaceshooterasteroids

asteroids make framerate dip after too many have been created

I have a top down space shooter with asteroids that are randomly generated around the player in a 1000 by 1000 space. when the player gets close to the edge in any direction of the asteroid field it will create another 1000x1000 field of asteroids for the player to fly through.

so after creating 6 or so fields of asteroids the game starts to slow down. I want the player to be able to return to a spot it has previously been and everything is in the same place so I can't just destroy a field when the player leaves it. this is a code i have tried. there's a big trigger collider around each asteroid, so when the player gets close to one it renders. but it locks the game up at the beginning. I'm unsure how to free up resources. Whats a good way to do this? thanks!

 void OnTriggerStay(Collider other) {
     if (other.tag == "Player") {
         gameObject.renderer.enabled = true;
     } else {
         gameObject.renderer.enabled = false;
     }

 }

Comment
Add comment · Show 6
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 Linus · Nov 08, 2014 at 06:57 AM 0
Share

1000x1000x6 = ALOT

Dont use OnTriggerStay Pool the renderers in a BuiltIn-Array http://wiki.unity3d.com/index.php?title=Which_$$anonymous$$ind_Of_Array_Or_Collection_Should_I_Use?

Did I mention 1000x1000x6 is alot?

Having smaller sections of astroids will help.

I re did this comment 3 times, I just give up

avatar image tanoshimi · Nov 08, 2014 at 08:07 AM 1
Share

So each asteroid field is 1000units high x 1000units wide? That's O$$anonymous$$, but how many objects are there in each of those fields? Are you using physics for collisions with the asteroids? If your asteroids are moving, why would the player expect everything to be in exactly the same place when they returned?

avatar image FairGamesProductions · Nov 08, 2014 at 11:31 AM 0
Share

You need to look into Occlusion Culling if you have Unity pro.

avatar image Vetpetmon · Nov 30, 2014 at 03:31 AM 0
Share

$$anonymous$$ake a area limit around the player or you could also put in a field limit. It would delete an older field, but that would take too long.

avatar image Eric5h5 · Nov 30, 2014 at 06:31 AM 1
Share

You need to look into Occlusion Culling if you have Unity pro.

No...occlusion culling will do nothing; this is a top-down shooter. Also randomly generated, so you couldn't use occlusion culling even if you wanted to for some reason.

Show more comments

1 Reply

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

Answer by Scribe · Nov 08, 2014 at 01:01 PM

As tanoshimi mentioned, unless asteriods are very rare, the player is not going to remember what position they were in when they left an area, so how about a different approach, where you have a set field of asteroids that simply follow the player around within some distance, this way you can utilise pooling and can know that you will never exceed a certain number of asteriods, see the following code:

 public int instances = 1000;
 public float dist = 100;
 float buffer;
 Transform[] instanceArray;
 public Transform instancePrefab;
 float height;
 Transform thisTrans;
 public float timeStep = 0.2f;
 Vector3 deltaPos;
 Vector3 lastObjPos;
 
 void Start () {
     thisTrans = transform;
     height = thisTrans.position.y;
     buffer = dist + (0.1f*dist);
     instanceArray = new Transform[instances];
     for(int i = 0; i < instances; i++){
         Vector2 pos = Random.insideUnitCircle * dist;
         instanceArray[i] = Instantiate(instancePrefab, new Vector3(pos.x, height, pos.y)+thisTrans.position, Quaternion.Euler(0, Random.Range(0f, 360f), 0)) as Transform;
     }
     lastObjPos = thisTrans.position;
     InvokeRepeating("UpdatePool", timeStep, timeStep);
 }
 
 void UpdatePool(){
     deltaPos = thisTrans.position-lastObjPos;
     for(int i = 0; i < instances; i++){
         Vector3 dir = (instanceArray[i].position - thisTrans.position);
         if((instanceArray[i].position - thisTrans.position).sqrMagnitude < (buffer*buffer)){
             continue;
         }
         instanceArray[i].position = (thisTrans.position-dir)-deltaPos;
     }
     lastObjPos = thisTrans.position;
 }

You can actually have a very small amount of asteriods using this method, as long as they cover your viewing area. You could even randomise there size and speed when the position is recalculated as well around line 36 to give the effect of there being more than there are!

Hopefully that helps

Scribe

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 Ace22 · Nov 08, 2014 at 08:53 PM 0
Share

this works really well. Ill use this for now, but if I make certain asteroids mine-able I'll have to think of something different. thanks!

avatar image Scribe · Nov 09, 2014 at 01:28 PM 1
Share

If mineable asteriods are not as common you could handle them in a similar fashion to other stationary or unique objects and simply use this to control the bulk of the rigidbody asteriods. If you think your question has been answered please remember to mark as accepted so others know where to find answers (and I get points ^^)

alt text

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

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

7 People are following this question.

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

Related Questions

Box Collider bounce causing rigidbody to change rotation -1 Answers

2D Shoot at Mouse Position; how to rotate towards the mouse? 1 Answer

Creating a wrap around bullet effect like Asteroids. 1 Answer

I desparately need a space skybox 3 Answers

Camera Setup for Top Down Shooters 3 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