Flappy Pig – Obstacle Object Pooling
Day 078 #100DaysOfCode
Day 042 #100DaysOfGameDev
- create new PostPool script attached to GameControl
- initialize a new public postPoolSize int variable set to 5
- declare a new public GameObject called postPrefab to call the Posts prefab with
- declare a new private GameObject[] array called woodenPosts
- initialize a new private Vector2 called objectPoolPosition and set it to appear off-screen
- set up woodenPosts array in Start
- initialize a new public spawnRate float variable initialized to 4f
- declare a new private timeSinceLastSpawn float variable
- initialize a new public postMin float to set the lowest spawn point for wooden posts
- initialize a new public postMax float to set the highest spawn point for wooden posts
- initialize a new private spawnXPosition float to set the X position of all newly-spawned posts, which will always be the same off-screen X position
- add deltaTime to timeSinceLastSpawn to create a timer to check against spawnRate to see if a Post should be created
- reset timeSinceLastSpawn timer
- create random Y position using postMin and postMax
- initialize a new private currentPost int to track the array objects and know which post will be moved from the pool
- move current post in array to new position off-screen to be ready to enter game and advance to next post in array