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?
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)
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!!
a little bit laggy but if you have better performance on you comp or laptop or whatever.. it can be run smoothly no lag...
do you mean the number of colliders you have is not a problem?
Totally no problems..... it's part of the development.. but sometimes it can be glitch.. but no problems at all..
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?
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;
}
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!
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!!!