Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 11 Next capture
2021 2022 2023
1 capture
11 Jun 22 - 11 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
1
Question by awplays49 · Jul 12, 2015 at 10:03 PM · 2dnot workingchunksterain

Render area not working correctly

Hello!

Ive been working on a feature for my game where a square shaped area around the player that contains chunks would be rendered, but outside of that, chunks will not be rendered. This is for lag reduction purposes and will make the game smoother looking.

Code:

 void LateUpdate () {
         UpdateRenderArea ();
     }
 
 int RoundNumberToNearest (float n, int r) {
         float divided = n / r;
         int rounded = Mathf.RoundToInt (divided);
         int multiplied = rounded * r;
         return multiplied;
     }
 
 void UpdateRenderArea () {
         int playerX = RoundNumberToNearest (player.transform.position.x, chunkSize);
         int playerY = RoundNumberToNearest (player.transform.position.y, chunkSize);
         int playerXClamp = Mathf.Clamp (playerX / chunkSize - renderDistance, 0, chunkCountX - 1);
         int playerYClamp = Mathf.Clamp (playerY / chunkSize - renderDistance, 0, chunkCountY - 1);
         int playerXClamp2 = Mathf.Clamp (playerX / chunkSize + renderDistance, 0, chunkCountX - 1);
         int playerYClamp2 = Mathf.Clamp (playerY / chunkSize + renderDistance, 0, chunkCountY - 1);
         Debug.Log (playerXClamp + ", " + playerXClamp2 + ", " + playerYClamp + ", " + playerYClamp2);
         bool [,] renderArea = new bool [chunkCountX, chunkCountY];
         for (int x = playerXClamp; x < playerXClamp2 + 1; x ++)
         {
             for (int y = playerYClamp; y < playerYClamp2 + 1; y ++)
             {
                 renderArea [x, y] = true;
             }
         }
         for (int x = 0; x < chunkCountX; x ++)
         {
             for (int y = 0; y < chunkCountY; y ++)
             {
                 chunks [x, y].SetActive (renderArea [x, y]);
             }
         }
     }

The rest has been cut out, basically, though, chunks are put through a parenting system when blocks are made. all chunks are made, and then deactivated before one frame. the bottom left chunk, for example, would be in the array as "chunks [0, 0]" and it's name is "chunk 1-1".

However, when setting the render distance lower, it seems to favor the left side of the map more? it overall is just not working too.

Comment
Add comment · Show 5
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 awplays49 · Jul 13, 2015 at 12:29 AM 0
Share

After 2 straight hours of bug fixing, I must go to bed. Goodnight, Ill answer any comments in the morning.

avatar image awplays49 · Jul 14, 2015 at 11:55 PM 0
Share

Another long day of staring at this code. Goodnight.

avatar image zach-r-d · Jul 15, 2015 at 05:15 AM 0
Share

Just to be clear: your terrain is in the xy plane, not the xz plane?

avatar image awplays49 · Jul 15, 2015 at 12:01 PM 0
Share

right. thanks for responding by the way :)

avatar image _dns_ · Jul 15, 2015 at 02:45 PM 0
Share

Hi, when I have this kind of problems, I use a lot of Debug.DrawLine to figure out visualy what I'm doing wrong.

1 Reply

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

Answer by zach-r-d · Jul 15, 2015 at 07:53 PM

I'm fairly certain the problem can be fixed by doing these two things:

  1. Replace the definitions of the variables in UpdateRenderArea to the following:

     float playerX = player.transform.position.x/chunkSize + .5f; // +.5f calculates from center of chunk rather than corner
     float playerY = player.transform.position.y/chunkSize + .5f;
     int playerXClamp = Mathf.Clamp (Mathf.FloorToInt(playerX - renderDistance), 0, chunkCountX - 1);
     int playerYClamp = Mathf.Clamp (Mathf.FloorToInt(playerY - renderDistance), 0, chunkCountY - 1);
     int playerXClamp2 = Mathf.Clamp (Mathf.FloorToInt(playerX + renderDistance), 0, chunkCountX - 1);
     int playerYClamp2 = Mathf.Clamp (Mathf.FloorToInt(playerY + renderDistance), 0, chunkCountY - 1);
    
    
  2. Use <= instead of < for the condition of each of the for loops.

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

23 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

Related Questions

Unity2D Raycast not working 1 Answer

2D Animation does not start 1 Answer

I need to instantiate a bunch of objects without performance dips 1 Answer

2D Infinite Terrain Generation Performance Issues 0 Answers

using UnityEngine.UI; 'Not necessary' although I need to use 'Text' and '.text'. Also unable to drag-and-drop the Text from my canvas into the 'text' slot. 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