• 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 MaxieNoob · Feb 15 at 01:29 AM · cameraoptimization

Need help with my view distance script, and if i can either A. optimize it or B. split a table into multiple pieces.

So, what im trying to accomplish here is to make a view distance script for the camera, and im trying to optimize it, as i have about 175k objects in the scene(an open-world game), and its laggy each frame it detects the view distance and i want to fix it somehow. Please help!

here is my code:

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class ViewDistance : MonoBehaviour
 {
     // Start is called before the first frame update
     public GameObject Parent = null;
     public WorldValues values;
     GameObject RootPart = null;
     Vector3 OldPlayerPos;
     public float MaxDistance;
     public float MaxItems;
     public float TickSpeed = 1;
     bool cantickagain = true;
 
     public bool cam;
     void Start()
     {
         RootPart = values.RootPart;
     }
 
     // Update is called once per frame
     void Update()
     {
         if (OldPlayerPos != values.MainCamera.transform.position)
         {
             if (cantickagain)
             {
                 foreach (Transform object1 in Parent.transform)
                 {
                     Vector2 camdis = new Vector2(values.MainCamera.transform.position.x, values.MainCamera.transform.position.z);
                     Vector2 objdis = new Vector2(object1.transform.position.x, object1.transform.position.z);
                     if (Vector2.Distance(camdis, objdis) > MaxDistance)
                     {
                         object1.gameObject.SetActive(false);
                     }
                     else
                     {
                         object1.gameObject.SetActive(true);
                     }
                 }
                 OldPlayerPos = values.MainCamera.transform.position;
             }
         }
 
 
         if (cantickagain)
         {
             IEnumerator tick()
             {
                 cantickagain = false;
                 yield return new WaitForSeconds(TickSpeed);
                 cantickagain = true;
             }
             StartCoroutine(tick());
         }
     }
 }
 






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

1 Reply

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

Answer by Captain_Pineapple · Feb 15 at 09:31 PM

There are multiple things you can do.

Though the best you can do to begin with is to actually check the profiler what actually takes up all the performance. You should never optimize blindly.


Howevery an easy improvement would be to not get the Distance but the squared distance instead.

  if (camdis - objdis).sqrMagnitude > MaxDistance

where in your start function you add the line:

   MaxDistance *= MaxDistance

This will greatly improve performance but you might have to turn MaxDistance into a long

Why is this faster you ask? -> Because distance is the squareroot of the squares of the vector elements. Calculating the squareroot is really really slow.


Apart from this you should consider to group your objects on a grid. Then you only have to check your players position if it moves into a new grid element. So for example your grid has a size of 50x50 units. You have a 2D list of all grid elements which in turn each contain a reference to all objects which are positioned within. (This can already be setup in your scene so takes up no performance at runtime)

Assuming your player starts at grid (0,0). At startup you can disable all objects in grid elements which are lets say between -4 and 4 in x and y direction.

Then when your player for example leaves grid element (0,0) for grid element (0,1) you disable all objects in grid elements (-4 to 4, -4) and enable all objects in grid elements (-4 to 4, 5).

This can then be further optimized by creating a routine which only enables and disables like 5 objects per frame so that your game is not overloaded on the instance you move from one grid to another.

Games like minecraft for example use similar system. There the grid elements are called "chunks".


Hope this helps, let me know if something was unclear.

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 MaxieNoob · Feb 16 at 01:29 AM 0
Share

Thanks for the help, i think i will use the grid system you mentioned, as i like to use things i understand so i can change them later on if needed, and im basically a newborn at coding and dont get what really happens to make the performance easier in the first solution, but i think the grid will help a lot with performance, as instead of calculating say, 200k objects i'd calculate 10k or something, thanks for the help! This is like, my first real question on the site.

avatar image Captain_Pineapple MaxieNoob · Feb 16 at 08:23 AM 0
Share

cool, good luck then :)


Just to clarify what the first part is about:


x^2 is cheap to calculate. This is just y = x*x right?

But the inverse of this is really difficult on a computer. so to get x from y you need x = sqrt(y)

Now if you calculate the length of a vector, which in your case is your distance value then unity calculates this below the hood:

    d = sqrt(x*x + y*y + z*z)

And then you compare d to some distance which is your threshold.


Now Unity offers the sqrMagnitude which is (for some vector v) just

   v.sqrMagnitude = v.x*v.x + v.y*v.y + v.z*v.z

You can see that unity does not calculate the squareroot here. So before you had the equation:

   Maxdist > v.magnitude

But faster is to write:

   Maxdist*Maxdist > v.sqrMagnitude

As the sqrt is not calculated here.

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

209 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 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

Occlusion Culling for Portals 0 Answers

Cost of Camera.main 0 Answers

Rendering to screen with two different resoulution. 0 Answers

Deleting GUI Layer creates GC Allocation? 1 Answer

How Do I Not Render Objects That My Player Doesn't See 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