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.