Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 Wenon · Apr 30, 2018 at 04:20 PM · instantiateobjectdestroyactivefar

Destroy and instantiate far objects

Hello, I have simply question, I want to destroy far map objects (or just make them inactive) and instantiate (or set active) them again when I'm getting closer. What will be the best for performance. Checking for all objects vector3.distance or make huge sphere around of player and then OnTriggerEnter and Exit? Or somethink else?

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
1
Best Answer

Answer by TreyH · Apr 30, 2018 at 04:42 PM

The trigger route would require that every ignored / distant object has a trigger collider, which might not be fun to set up.

There are camera settings for this sort of thing, so I don't know how much effort you actually need to do manually. Unity does quite a bit for you out of the box.

For your question, Octrees are useful for these sorts of things, and Unity's own Terrain system uses them iirc. With an Octree (or Quadtree in 2D), you break the world into a series of blocks. Each block is then checked for whether or not it contains a number of objects (a number you set, we'll call this N). If that block contains N or more objects, it's subdivided into 8 (or 4 for 2D) smaller blocks, which then undergo the same check. Performance-wise, these trees can be produced in near-linear time.

How often you produce / update the tree is up to you. Once a tree is produced, you wouldn't check for distance to individual objects -- you would check distance between the block with your camera / player versus other blocks, disabling everything within blocks that are too far.

Comment
Add comment · Show 8 · 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 Wenon · Apr 30, 2018 at 05:45 PM 0
Share

Sounds logical but one question came to my $$anonymous$$d, is it necessary to destroy objects if they are static, without rigidbody and scripts and they are out of camera? Will somehow unity to process them? Octrees at this time are to complicated to make for me.

The trigger route would require that every ignored / distant object has a trigger collider, which might not be fun to set up.

Is it not enough to just add trigger collider to objects? Even if I just started making map and placing objects?

avatar image TreyH Wenon · Apr 30, 2018 at 05:55 PM 0
Share

If you're O$$anonymous$$ with everything having a trigger collider, then your sphere solution would work just fine. I didn't know how large your scene was going to be (100 objects vs. 100,000 etc.).


I doubt that you'll need / want to destroy an object outright. Disabling an object will suspend it from physics, stop its Update calls, prevent rendering, etc.

One performance consideration is that you should probably not use Vector3.Distance or a Vector3 instance's .magnitude for your distances. Both of those involve square root calculations which are slow, but you can use .sqr$$anonymous$$agnitude to avoid this.

avatar image Wenon TreyH · Apr 30, 2018 at 06:13 PM 1
Share

$$anonymous$$y scene will be big I think. But I will have to add to every object box colliders manually so add one more tagged as trigger will no be an extra large job. I just hope that ontriggerexit will not miss any object by mistake. About distance, you are right. I will change every vector3.distance to sqrmagnitude in my scripts. Thank you

Show more comments
avatar image
0

Answer by SharkoFR · Apr 30, 2018 at 04:57 PM

The best way is simply to use LOD: https://docs.unity3d.com/Manual/LevelOfDetail.html

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 Wenon · Apr 30, 2018 at 05:46 PM 0
Share

The camera is from top to bottom like in gta1, I think lod are not necessarily

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

111 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

Related Questions

how to raycast instantiated game characters 1 Answer

How to destroy instantiated objects. 1 Answer

Destroying object using his name and raycast 2 Answers

Instantiating droppable items on destroy game object 1 Answer

Raycast Specific Object/Instantiate Explosion 1 Answer


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