Hi,
i have a problem here which i don’t really understand.
I created a simple AI which avoids obstacles, follows a path, attacks enemies, helps friends, escorts objects, etc.
While implementing relationships between npc-groups/-factions i attached a trigger object as child. Now whenever a npc enters the trigger of another npc, their relationship is checked and both NPCs react accordingly (attack, help, ignore, etc.).
All that works great except for the trigger object resulting in a massive slowdown in some cases when avoiding obstacles, which doesn’t make any sense. Obstacle avoidance is done by randomized raycasting in front of npc. According to relative position and distance of a raycast hit, the npc moves/turns away from the obstacle. “Raycast hit Trigger” is disabled in Physics options.
Here’s a screenshot where the problem occurs:
The npc is above the cubes, framerate dropped from 400 to 2 fps. As soon as the npc reaches a certain point to the left of the cubes, framerate goes up to 400 again. The cubes are standard Unity cubes (GameObject → Create Other → Cube) with no additional scripts or anything else attached. The npc has a sphere trigger attached.
This is the npc:
Now i tried to debug the problem, so i started removing scripts and components from the npc. In the end only the rigidbody, Capsule Collider and the “triggerBox” childobject with a Sphere Trigger attached were left. Now i manually placed the remains of the npc above the cubes…and guess what, i still get a MASSIVE slowdown in the first few frames when the game loads. When manually moving the npc around above the cubes in the editor (while the game is running) i get a slowdown to 2fps for a few frames and then the framerate goes up again until i move the npc again.
Once i remove the rigidbody from the npc, in don’t get a slowdown anymore.
I’m really stuck here.
Is there a limit to trigger sizes, are “large” triggerzones a no-no? The showcased npc currently uses a sphere trigger with a radius of 100. I also tried using a cube trigger with scale 200/200/200 but got the same slowdown problem (cube->cube intersection is faster than cube->sphere if i remember correctly).
Setting the Physics Iterations from 6 to 1 didn’t help either.
Profiler gives me ~700ms for “Physics.Simulate” when a slowdown happens.
NPC Hoover Trigger sets the “target” gameObject in “NpcHoover”, once a npc entered the trigger zone.
NpcHoover then attacks/escorts/follows/etc the target.
However i even get the slowdown when completely removing all custom scripts/components and only leave the rigidbody and colliders/triggers intact.
How would i effectively use a raycast for this? I mean i would have to do a raycast for each npc then. That would lead to npc² raycasts which is quite much? And even if i get it to work with raycasts, i still have the trigger issue. I might need a trigger zone somewhen in future development.
Made some more tests. If i have only the terrain, obstacles/cubes and one npc in the scene, and then manually move the npc around in the editor while playing, i still get the slowdown. What’s going on here scratches head
Thanks for the answers so far!
[edit] Don’t know if this matters, but with unity 3.3 i get ~400 fps max, and with 3.4 i get ~100 fps without changing anything. Is this normal behaviour? (Project was 3.3, i only upgraded because of the trigger bug thingy as 3.3 was running fine for everything else )
[edit2] If i remove the terrain (or deactivate the terrain collider), everything works as expected without any slowdowns. Hmmm…
I guess it would be a lot of ray casts, but ray casts won’t go through walls and objects like a trigger would. The trigger may also be slowing it down because every second it’s touching something, it’s checking for the tag of the object it’s touching, and it does this every frame (I don’t know how likely this is).