- Home /
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 ship and turns the individual asteroids collider off when it is too far from the ship,
but this process is slow.
I figure that the method i use to calculate Distance is too slow.
this 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 && ShipDistance < 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.
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.
thanks for pointing that out . i meant to write "Distance".
this is just a smaller pseudocode of the script im using
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. Everything the sphere detects would have it's collider turned on. You could have this run once a second or something to reduce the amount of calculations. You still might be in for a lot of calculations though depending on how many asteroids are nearby.
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 .
i cant find an implementation example . i wish they would be more clear on implementation of these things.
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 things 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 this -- 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 hit itself, but can hit 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.
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 .
Answer by Alanimator · May 01, 2014 at 03:57 PM
Ultimately , it is slower to be switching between convex and non convex meshes at runtime , especially in this 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.