Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 13dnizinski · Dec 17, 2013 at 03:50 AM · cameralagplaneclipping

Why does setting the Camera Clip Plane make my game run slow?

A little background: I am making a game similar to Minecraft with a three-dimensional array of cubes. This array is a 50 x 50 x 10 and whenever I turn Camera Clip Plane to only show cubes close to the player, the game tends to lag a lot. I have made one solution, but every 10 seconds, a very large lag occurs, but the game otherwise runs smooth. This was my fix:

 IEnumerator setVisibleRange(List<List<List<GameObject>>> blockList) {
     canScanRange = false;
     for(int r = 0; r < blockList.Count; r++) {
         for(int c = 0; c < blockList[r].Count; c++) {
             for(int d = 0; d < blockList[r][c].Count; d++) {
                 if(blockList[r][c][d] is GameObject) {
                     float dist = Vector3.Distance(blockList[r][c][d].transform.position, GameObject.Find("Player").transform.position);
                     if(dist > 50) {            
                         blockList[r][c][d].SetActive(false);
                     } else {
                         blockList[r][c][d].SetActive(true);
                     }
                 }
             }
         }
     }
     yield return new WaitForSeconds(10);
     canScanRange = true;
 }

My 'fix' has to run though EVERY element in the array which makes the lag happen. Is there a way for this lag to go away?

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 Kiloblargh · Dec 17, 2013 at 04:08 AM 1
Share

You can't make a game like $$anonymous$$inecraft using a 3-dimensional array of cubes, at least not one that will run smoothly on currently available hardware. You're simply doing it wrong and no amount of optimizing will help. If you want to make a voxel based game, read up on how voxel engines work first.

Just for fun, edit your script to make the array 64,000,000 x 64,000,000 x 256 and see what happens.

3 Replies

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

Answer by Eric5h5 · Dec 17, 2013 at 04:47 AM

50x50x10 is 25,000 objects, which is way too many. Even if they aren't all drawn (which is 25,000 draw calls), they all have to be culled every frame. You can find lots of answers about minecraft stuff in Unity with a search, and they will all tell you to write code that creates mesh chunks, rather than instantiating each cube as a separate GameObject. Creating mesh chunks will increase efficiency by orders of magnitude and you can ditch the SetVisibleRange function.

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
0

Answer by Spinnernicholas · Dec 17, 2013 at 04:03 AM

First off, Vector3.Distance is very, very slow because of the square root involved. Use Vector3.sqrMagnitude and compare it to 50*50. That should have an immediate effect.

Comment
Add comment · Show 5 · 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 Spinnernicholas · Dec 17, 2013 at 04:03 AM 0
Share

I'll post more in a little bit.

avatar image Eric5h5 · Dec 17, 2013 at 04:40 AM 0
Share

Not true; CPUs have evolved since the '90s when square roots actually were slow. On any semi-modern desktop CPU (and including some AR$$anonymous$$ chips), square roots are a hardware instruction and you can do tens of millions of them per second. ($$anonymous$$ine will do nearly 100 million per second in Unity, but some of that is really the function call overhead rather than the actual square root operation.) In most cases you won't see any difference between .magnitude and .sqr$$anonymous$$agnitude unless you're doing huge amounts of them. (Although there are huge amounts in this case.)

Also, don't bother trying to avoid division, it's just as fast as addition and subtraction these days....

avatar image 13dnizinski · Dec 17, 2013 at 04:41 AM 0
Share

That helped reduce the lag by A LOT. Thank you so much!

avatar image 13dnizinski · Dec 17, 2013 at 06:05 AM 0
Share

Is there a way to set all the non-rendered objects (using camera clipping plane) to inactive?

avatar image Kiloblargh · Dec 17, 2013 at 06:54 AM 0
Share
 if ( !its.renderer.isVisible ) { its.gameObject.SetActive (false); } 

But this whole approach is still a lost cause; you should mark Eric's answer correct and do some more research before starting over rather than putting more time into it.

avatar image
0

Answer by 13dnizinski · Dec 17, 2013 at 04:22 PM

Okay - I found an answer: If I parent a bunch of objects to a 'chunk' gameobject, Instead of iterating though all the blocks, I would only have to set specific chunks visible, which in turn would set all their children blocks to visible.

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

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

19 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

Related Questions

Reduce water's reflection camera max clipping plane 1 Answer

GUI on one camera visible to other camera 1 Answer

Problem with 3D objects rendering in front of closer objects. 1 Answer

Oblique Frustum Clipping 0 Answers

Shadow flicker 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