- Home /
asteroids make framerate dip after too many have been created
I have a top down space shooter with asteroids that are randomly generated around the player in a 1000 by 1000 space. when the player gets close to the edge in any direction of the asteroid field it will create another 1000x1000 field of asteroids for the player to fly through.
so after creating 6 or so fields of asteroids the game starts to slow down. I want the player to be able to return to a spot it has previously been and everything is in the same place so I can't just destroy a field when the player leaves it. this is a code i have tried. there's a big trigger collider around each asteroid, so when the player gets close to one it renders. but it locks the game up at the beginning. I'm unsure how to free up resources. Whats a good way to do this? thanks!
void OnTriggerStay(Collider other) {
if (other.tag == "Player") {
gameObject.renderer.enabled = true;
} else {
gameObject.renderer.enabled = false;
}
}
1000x1000x6 = ALOT
Dont use OnTriggerStay Pool the renderers in a BuiltIn-Array http://wiki.unity3d.com/index.php?title=Which_$$anonymous$$ind_Of_Array_Or_Collection_Should_I_Use?
Did I mention 1000x1000x6 is alot?
Having smaller sections of astroids will help.
I re did this comment 3 times, I just give up
So each asteroid field is 1000units high x 1000units wide? That's O$$anonymous$$, but how many objects are there in each of those fields? Are you using physics for collisions with the asteroids? If your asteroids are moving, why would the player expect everything to be in exactly the same place when they returned?
You need to look into Occlusion Culling if you have Unity pro.
$$anonymous$$ake a area limit around the player or you could also put in a field limit. It would delete an older field, but that would take too long.
You need to look into Occlusion Culling if you have Unity pro.
No...occlusion culling will do nothing; this is a top-down shooter. Also randomly generated, so you couldn't use occlusion culling even if you wanted to for some reason.
Answer by Scribe · Nov 08, 2014 at 01:01 PM
As tanoshimi mentioned, unless asteriods are very rare, the player is not going to remember what position they were in when they left an area, so how about a different approach, where you have a set field of asteroids that simply follow the player around within some distance, this way you can utilise pooling and can know that you will never exceed a certain number of asteriods, see the following code:
public int instances = 1000;
public float dist = 100;
float buffer;
Transform[] instanceArray;
public Transform instancePrefab;
float height;
Transform thisTrans;
public float timeStep = 0.2f;
Vector3 deltaPos;
Vector3 lastObjPos;
void Start () {
thisTrans = transform;
height = thisTrans.position.y;
buffer = dist + (0.1f*dist);
instanceArray = new Transform[instances];
for(int i = 0; i < instances; i++){
Vector2 pos = Random.insideUnitCircle * dist;
instanceArray[i] = Instantiate(instancePrefab, new Vector3(pos.x, height, pos.y)+thisTrans.position, Quaternion.Euler(0, Random.Range(0f, 360f), 0)) as Transform;
}
lastObjPos = thisTrans.position;
InvokeRepeating("UpdatePool", timeStep, timeStep);
}
void UpdatePool(){
deltaPos = thisTrans.position-lastObjPos;
for(int i = 0; i < instances; i++){
Vector3 dir = (instanceArray[i].position - thisTrans.position);
if((instanceArray[i].position - thisTrans.position).sqrMagnitude < (buffer*buffer)){
continue;
}
instanceArray[i].position = (thisTrans.position-dir)-deltaPos;
}
lastObjPos = thisTrans.position;
}
You can actually have a very small amount of asteriods using this method, as long as they cover your viewing area. You could even randomise there size and speed when the position is recalculated as well around line 36 to give the effect of there being more than there are!
Hopefully that helps
Scribe
this works really well. Ill use this for now, but if I make certain asteroids $$anonymous$$e-able I'll have to think of something different. thanks!
If $$anonymous$$eable asteriods are not as common you could handle them in a similar fashion to other stationary or unique objects and simply use this to control the bulk of the rigidbody asteriods. If you think your question has been answered please remember to mark as accepted so others know where to find answers (and I get points ^^)
Your answer
Follow this Question
Related Questions
Box Collider bounce causing rigidbody to change rotation -1 Answers
Showing information of incoming gameobject 1 Answer
top down shooter help 0 Answers
Make joystick rotate object 0 Answers
Scrolling space shooter - making enemies drop currency 1 Answer