Reputation: 21
The game consists of a player and a ground. Where the ground is a cube being instantiated relative to the cube before, either on its right or left. The player's goal is to swipe or move in the correct direction and to land on the ground(cube).
However, I have only managed to instantiate a limited amount of cubes without having Unity crash as a result of an endless loop of instantiating cubes.
I have also tried to destroy a cube from the bottom when no longer in the camera view(using the OnBecomeInvisible()
method) in order to be able to instantiate a cube at the top of the limited amount of cubes instantiated, as to give a feeling of endless gameplay, but as the camera follows the player, some cubes appear in the scene on either far left or right but as the camera gets centered once again they are no longer visible, which in turn destroys the cube before even the player lands on the cube or view it.
Would making the ground scroll, and when it drops below a certain Y value, it gets destroyed as it is out of the camera's view, work? I have tried it already but I feel like it is supposed to work.
Are there any other methods?
Upvotes: 1
Views: 272
Reputation: 9824
I do not have any code to offer, just ideas that you would have to implement yourself.
I think the Data Structure you need is something similar to a Queue with Size limit: You add all new Cubes with Enqueue. If you exceed the size target during Enqueue, you Dequeue (and then remove from the scene) whatever is exceeding the limit.
Unfortunately I doubt you can use any of the existing Queue implementations like Queue[T] - at least not without overriding or hiding the Enqueue function with your own implementation - as they are not designed to enforce a limit. Much less enforce it by Dequeue-ing excess and running some Unity on code on whatever it Dequeues that way.
Upvotes: 1