• 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
Question by Alanimator · Apr 22, 2014 at 07:25 PM · c#collisionphysicsrigidbodydistance

Handling colliders on hundreds of asteroids (not procedural asteroids) ...URGENT

I have 480 asteroids in my scene . it is not a good idea to add colliders to every single object so I wrote a little script that takes in the distance of the asteroid from my s$$anonymous$$p and turns the individual asteroids collider off when it is too far from the s$$anonymous$$p,

but t$$anonymous$$s process is slow.

I figure that the method i use to calculate Distance is too slow.

t$$anonymous$$s is a standalone build and it can run up to 70 FPS but when the asteroids are in the scene it can fall to as low as 15 FPS .

         public  MeshCollider _MeshCollider; 
     public  MeshRenderer _MeshRenderer;
         private GameObject myPlayer;
 private Float Distance;
 
 
     void Start()
     {
         
         _MeshCollider = gameObject.GetComponent<MeshCollider> ();
 
         //-----------------------------------------------------------------------------------------------------
         _MeshRenderer = gameObject.GetComponent<MeshRenderer> ();
 
             }
 
 
 
 void Update ()
 {
 
 
 if(! myPlayer)
 {
  myPlayer = GameObject.FindWithTag("myPlayer"); // wont eat up MS too much when called once
 
 Distance = Vector3.Distance(myPlayer.transform.position ,gameObject.transform.position);
 
 
 //-------------------------------disable collider over distance using player distance-----------
         if (Distance < 100) {
             if (_MeshCollider.collider.enabled == false) {
                 _MeshCollider.collider.enabled = true;
                         }
             _MeshCollider.convex = true;
                 } 
 
         if (Distance > 100 && S$$anonymous$$pDistance < 1000) {
             if (_MeshCollider.collider.enabled == false) {
                 _MeshCollider.collider.enabled = true;
                         }
             _MeshCollider.convex = false;
 
                 }
         if(Distance > 1000) {
             _MeshCollider.collider.enabled = false;
                 }
 
             //-------------------------------------------------------------------------------------- 
 
 
 }
 
 }

is there another way to allow eaach asteroid to take collisions and without slowing down the whole game .

my method works but it is too slow.

Comment

People who like this

0 Show 2
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 getyour411 · Apr 23, 2014 at 12:10 AM 0
Share

What does myPlayer (a GameObject set via FindWithTag) have to do with 100, 200 or 1000? Did you intend to close out that first if(!myplayer) after the set? The logic looks off.

avatar image Alanimator · Apr 23, 2014 at 12:21 AM 0
Share

thanks for pointing that out . i meant to write "Distance".

this is just a smaller pseudocode of the script im using

3 Replies

· Add your reply
  • Sort: 
avatar image

Answer by OtsegoDoom · Apr 22, 2014 at 07:35 PM

You could use Physics.OverlapSphere (https://docs.unity3d.com/Documentation/ScriptReference/Physics.OverlapSphere.html) on your player.

The radius of the sphere would be the max distance you want for your asteroid colliders. Everyt$$anonymous$$ng the sphere detects would have it's collider turned on. You could have t$$anonymous$$s run once a second or somet$$anonymous$$ng to reduce the amount of calculations. You still might be in for a lot of calculations though depending on how many asteroids are nearby.

Comment
Alanimator

People who like this

1 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 Alanimator · Apr 22, 2014 at 08:51 PM 0
Share

the asteroids are at least 200 units away from the next closest asteroid so i guess there would not be many calculations if i used this method.

I have never used that method before but i will try it out and let you know how that goes .

avatar image Alanimator · Apr 23, 2014 at 12:16 AM 0
Share

i cant find an implementation example . i wish they would be more clear on implementation of these things.

avatar image

Answer by Owen-Reynolds · Apr 22, 2014 at 09:32 PM

Since you're OK with colliders turned off, I'm assuming asteroids can go through each other (like the game.) Some t$$anonymous$$ngs to try:

o Use sqrMagnitude in the distance calculations (very minor speed-up.)

o replace Mesh colliders with Sphere, or compound.

o As Ostego writes, do the math once/second. Or, better check 10 asteroids/frame (so each asteroid gets checked about 1nce/sec.)

o Try it without t$$anonymous$$s -- colliders always turned on. Is that really so slow?

o Same as before -- colliders always on. But put them all on a layer that doesn't $$anonymous$$t itself, but can $$anonymous$$t the player.

o Use the giant trigger sphere around the player trick. Where enter/exit flips collider status.

o Instead of flipping colliders, set too-far asteroids to inactive.

Comment
Alanimator

People who like this

1 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 Alanimator · Apr 23, 2014 at 12:05 AM 0
Share

sphere colliders dont reduce my FPS by much but the collision is not what i need , if the player shoots at one of the asteroids they may see an impact explosion outside of the visual bounds of the asteroid , which would not be good .

I am running in a tremendously slouchy and weak 8 year old 32 bit HP g60 computer with a 8 year old Graphics card.

I cant use compound colliders for this either.

i fire projectiles that can travel over 600 units from the player.

so the projectile need to be able to collide with asteroids that are at most 1000 units away.

the colliders are set to convex and my FPS just falls to as low as 15 and lower.

Cant add anything else to my player , it would bring about glitches .

i should had reached code freeze last week.

ummmm the above script is placed on each asteroid . i previously found the asteroids with a tag and added the script to them at runtime, but that method was slow .

avatar image

Answer by Alanimator · May 01, 2014 at 03:57 PM

Ultimately , it is slower to be switc$$anonymous$$ng between convex and non convex meshes at runtime , especially in t$$anonymous$$s case , so i just left the mall as convex and toggles their renderer and collider state depending on distance . Sqr magnitude would be a bit faster but it is fine as is now.

Comment

People who like this

0 Show 0 · 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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

22 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

Related Questions

Restrict two objects from getting too close. 1 Answer

Why can't my enemies walk up an incline? 1 Answer

Issue with adding force and lerping position? Possible unity bug? 0 Answers

What is the best way to prevent a physics object from going through thin colliders? 2 Answers

How to change CC script to Rigidbody script 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