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 /
  • Help Room /
avatar image
3
Question by KaiLiao · Apr 22, 2017 at 04:11 PM · physicscolliders

Will too many colliders affect game performance?

If you have too many colliders in your scene (like thousands), is that going to be a problem or something?

  1. I'm thinking about the raycasting function, I don't know if I'm right, as I know about raycast, it has to check every colliders in your scene, and find intersections, so the more collders you have, the slower this function will be, and maybe other physics functions too... (correct me if I'm wrong)

  2. besides raycasting, does it affect other things that will make your game slow?

The way I'm doing this is because I'm making game AI, I think if I use sphere (or box) collider as trigger, to find out surrendering objects (using the call back function "OnTriggerEnter"), then I don't have to calculate the OverlapSphere every frame, especially I have thousands of them.

If this is a bad way of doing that, please give me other advise of how do you do with AI's perception. thanks a lot!!

Comment
Add comment · Show 4
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 Clementinethe2nd · Apr 22, 2017 at 04:15 PM 0
Share

a little bit laggy but if you have better performance on you comp or laptop or whatever.. it can be run smoothly no lag...

avatar image KaiLiao · Apr 22, 2017 at 10:58 PM 0
Share

do you mean the number of colliders you have is not a problem?

avatar image Clementinethe2nd KaiLiao · Apr 23, 2017 at 03:14 AM 0
Share

Totally no problems..... it's part of the development.. but sometimes it can be glitch.. but no problems at all..

avatar image KaiLiao · Apr 23, 2017 at 04:25 AM 0
Share

okay, thank you! that's the anwserI'm looking for. so, the way the "Raycast" works is not like what I thought? game engine's raycasting is a O(1) algorithm or something?

1 Reply

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

Answer by toddisarockstar · Apr 23, 2017 at 05:50 AM

Ive tested actually tested this on a project. Even on on a slower machine thousands of box colliders did not show any performance issues! but if you intend to make bunches of AI raycasting every frame it will lag! and sphere cast is much much worse. Use collisions whenever possible. in a past project adding secondary trigger collider in front af ai to replace a forward raycast saved my game. In the future if i would make another project with hundreds of AI i would create my own system of detections through codeing depending on the accuracy you needed.

if you truely want to pull off hundreds or thousands of AI smoothly you are going to need to think outside the box a bit. actually inside the box!!! LOL. if you ask the engine to calculate a distance within a cirlce it uses the Pythagorean theory. In the background it has to calculate a bunch of fancy square roots and stuff for a circle.
https://math.stackexchange.com/questions/42640/calculate-distance-in-3d-space

but if you have a box collider the CPU is only checking three greater/less than statements for your XYZ. so use box colliders if you can!

if you are asking about thousands of AI your allready asking for advise that is beyond a beginner developer's knowledge. and pry cant use unity standard setup.

for people building a game a with few or dozens this advise does not matter but if you want thousands. I would would personally recommend replacing Unity's vector 3 distance (that uses the Pythagorean theory on three coordinates) with a small easy custom function that checks an x and z in a box like manner every severally frames and if you need anything fancier like more accurate colliders, or you raelly need to use raycasting turn it on an off with player distance.

as far as raycasting goes,i'm not sure if unity pages though every object or what it is doing but i did test it and it does lag when done with multiple objects ever frame.I still think it is a usefull tool when not used for moven't every frame. anyone is free to input on this answer about raycasting.... but i can promise you one thing.

Your frames per second will crash and burn if you are raycasting everyframe with lots of objects!!!! and spherecast is worse.

     float fastdistance(Vector3 v1,Vector3 v2){
         float f;
         float f2;
         f=v1.x-v2.x;
         if(f<0){f=f*-1;}
         f2=v1.z-v2.z;
         if(f2<0){f2=f2*-1;}
         
         if(f>f2){f2=f;}
         //within a box shaped disatance in 2D!!
         
         return f2;
     }
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 KaiLiao · Apr 23, 2017 at 08:54 AM 0
Share

I wasn't expecting someone to do the test for me LOL, thank you thank you!! I think I might get the answer, just did some experiment... here's what I did: I placed 1000 object with their collider in a scene then I tested raycast function with only one gameobject (only one gameobject is raycasting), and then here's the results:


1000 box collider in a scene:
call one Raycast function takes : 0.00001335 seconds
call one RaycastALL function takes : 0.00006104 seconds
call one Spherecast function takes : 0.00019789 seconds


1000 sphere collider in a scene:
call one Raycast function takes : 0.00002098 seconds
call one RaycastALL function takes : 0.00005507 seconds
call one Spherecast function takes : 0.00019312 seconds


a empty scene with no collider:
call one Raycast function takes : 0.00000286 seconds
call one RaycastALL function takes : 0.00000501 seconds
call one Spherecast function takes : 0.00000453 seconds


so the conclusion is, the number of colliders you have in a scene DOES affect the Raycast function. 1000 colliders vs 0 collider is like 10x slower!! so maybe raycast really pages through every object I think... But if I let colliders do the finding surrendering things, and only use raycast in one or two gameobject, I think it's great! any thoughts?

and what do people usually do with AI (AI's vision perception I think), sorry if it's a stupid question, I'm a newbie to game making... just started my first project. thinks!

avatar image toddisarockstar KaiLiao · Apr 23, 2017 at 01:23 PM 0
Share

Sorry, When I seen your question i thought you had the Ai doing the raycasting and i was like Noooooo! if its just a couple objects making a raycast here and there it's pry not a real big deal. the testing i did was over a year ago for one of my projects that was similar. not sure how new you are but if the raycasting doesn't need to happen every frame, then limit it. In every game i put a couple sections in update to limit all the things that don't need to happen every frame.

 int timer;
 void Update () {
 timer++;
 if(timer>20){timer=0;
 
  // things happening here will only happen about a couple times a second
  // ins$$anonymous$$d of about 50 times a second depending on FPS!!!!
 
 }
 
 
 
 }
 

as far as vision perception for AI i would use a cobination of the two codes I have posted. its about the "lightest" way possible for them all to see you! after they trigger this preli$$anonymous$$ary code you could always do a more accurate check!!!

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

136 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

Related Questions

[RESOLVED] Two rigidbodies not colliding (even with traditional fixes, e.g. raycasting, collision detection) 2 Answers

thisCollider and otherCollider from collision.GetContacts() 1 Answer

Ball passes through the floor on movement 1 Answer

Can 'position squishing' be prevented when using Rigidbody.movePosition()? 0 Answers

Characters clipping through colliders? 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