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 Bincredible · Apr 14, 2017 at 11:47 AM · distanceplatformeractivegameobject.active

How do I only show objects near the player

Hi, I am making a platformer which has spikes and blocks for the player to jump over as the player continuously moves along the platform. However when I make large levels it makes the game run slow which makes it harder to play due to low fps. I am trying to find a way to make the blocks and spikes only appear if they are within a certain distance of the player because that's the only time they are needed and on the screen, however I am having a lot of trouble with it. I have tried different variations of the following method but I cannot get it to work the way I want. How can I make the obstacle's game objects active when within a certain distance and inactive when not within that distance? I have also included a picture of a level to make it more clear. Thanks

 var blocks : GameObject[];
 var spikes : GameObject[];
 
 function Start(){
     blocks = GameObject.FindGameObjectsWithTag("BlockObstacle");
     spikes = GameObject.FindGameObjectsWithTag("Spike");
 }
 
 function Update(){
     for(var blocksToDestroy : GameObject in blocks){
         if(transform.position.x - blocksToDestroy.transform.position.x < 20f){
             blocksToDestroy.transform.gameObject.SetActive(true);
         }
         if(transform.position.x - blocksToDestroy.transform.position.x > 20f){
             blocksToDestroy.transform.gameObject.SetActive(true);
         }
     }
     for(var spikesToDestroy : GameObject in spikes){
         if(transform.position.x - spikesToDestroy.transform.position.x < 20f){
             spikesToDestroy.transform.gameObject.SetActive(false);
         }
         if(transform.position.x - spikesToDestroy.transform.position.x > 20f){
             spikesToDestroy.transform.gameObject.SetActive(true);
         }
     }
 }

alt text

screen-shot-2017-04-14-at-124546.png (24.7 kB)
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 RibsNGibs · Apr 14, 2017 at 01:52 PM

It's a wide ranging question with lots of potential answers, but in your specific case I would make a single 1-D list of buckets, with each bucket representing a range of your map (perhaps half a screen's worth, maybe a quarter or full - kind of depends on how large the map is and how expensive your blocks are to update), and if possible simply parent all the blocks and spikes to these buckets.

Then, instead of looping through every single block and spike every frame, you loop through every bucket and test for distance, and activate/deactivate as necessary. If you were able to parent all the blocks and spikes under the buckets then they will all activate and reactive automatically with the bucket. If not, you can (when the bucket gets within or leaves the range only, not every single frame), just loop through all the blocks and spikes in the bucket and set the active state correctly.

A further optimization would be to check these buckets not every single frame, but intermittently via coroutine, since your character assumably isn't flying around at 100mph, so if buckets turn themselves on when 40 units away but your character only moves 20 units per second you can check only once every 2 seconds and be safe.

More optimization would be to only check the buckets that are adjacent to other active buckets unless your character regularly teleports.

If your map gets really, really, really huge, instead of buckets containing blocks and spikes, you make buckets containing buckets containing blocks and spikes, in which case you should google up a quad tree and implement a 1D version of it, but from your screenshot I think a simple list of ~10-30 buckets full of blocks and spikes hold be easily fast enough.

Comment
Add comment · Show 3 · 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 Bincredible · Apr 14, 2017 at 03:08 PM 0
Share

Hi thanks for the detailed answer, I understand what you are saying and that would solve my problem. However since I have anchored the blocks and spikes to the bottom of the screen, when I make them a child of a bucket then this prevents the anchor from taking effect and therefore this whole thing wouldn't work. Is there a way around this so I can still use this method as well as the anchors?

avatar image RibsNGibs · Apr 14, 2017 at 03:23 PM 0
Share

Yes, you can do it without parenting. Inside the bucket, keep a list/unorderedset/whatever of all blocks and spikes located within the bucket. When the bucket decides that it should activate or deactivate, have it loop through that list/set of blocks and spikes and activate/deactivate them.

avatar image Bincredible RibsNGibs · Apr 14, 2017 at 03:35 PM 0
Share

This is a brilliant solution, thanks a lot!

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

66 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

Related Questions

Do something with GameObject with certain distance 1 Answer

2D 360 degress platformer example needed 0 Answers

Cannot toggle active on gameobjects that are inactive 2 Answers

Distance calculation for Endless Platformer. 1 Answer

How To Keep GameObjects From Enabling Again? 2 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