Notifications
On Diet - A mobile 3D food catch game
Published 8 months ago
Game
Released
Available on
48
0
Platform(s)
Android
Behind the scenes report: A fun game to train your new year’s diet plan
I’d like to present a free fun “food catch” game built with Unity for Android devices, released on the Google Play app store. In this article, I’ll discuss some of the mechanics behind it as a short “behind the scenes” / “case study” report.
Note: This article is written for Unity 2017.2.

The game

The aim of the game is simple: First, you choose a diet plan (vegetarian / vegan / paleo / lactose / fructose / gluten/ low carb or a combination or these), then you’re trying to catch all the food items that fall from the sky you’re allowed to eat according to the chosen diet. There’s a highscore per diet plan.
Over time, food falls down faster. If you catch a wrong item or if you miss too much food, game’s over.
Plus, there’s an additional “free style” mode where you can just pick whatever food you like and in the end, the app will tell you how well you score on each one of the diet plans.
This game was in part inspired by some of the excellent 3D (food) models found in the Unity asset store.
You can download the game for free at the Google Play app store.

The main menu

The player enters the game through the main menu. Here, he can choose his diet plan(s). He can make a single selection by pressing a button, or multi-select by activating toggles (check-boxes), then clicking a start button.
The highscores will also be displayed on each one of the buttons.
All of the UI elements in this game are hand-crafted.

Raining food

All of the action happens in the main game scene. Here, food items fall from the top to the bottom.
Many of the food items – the most important part of the game – I found on the Unity asset store. Here are some excellent (free) food items, some of which I used in this game:
  • FREE Food Pack
  • Adorable 3D Food Set
  • Food & Grocery Items - Low Poly
  • 3D Bakery Object
Each food item gets an initial “tumble” effect. The movement vector, however, need to be updated each fame as the speed increases over time.
Here’s the relevant code:
public class ConstantMover : MonoBehaviour { private static float tumbleSpeed = 1.5f; private rb; void Awake() { this.rb = this.GetComponent<Rigidbody>(); } // "On Enable" instead of "Awake" for compatibility with "Lean Pool" void OnEnable () { Tumble (tumbleSpeed); } private void Tumble(float speed) { rb.angularVelocity = Random.insideUnitSphere * speed; } void FixedUpdate() { MoveWith (new Vector3 (0f, -1f, 0f), Global.foodMovingSpeed); } private void MoveWith(Vector3 movement, float speed) { movement = movement.normalized * speed * Time.deltaTime; rb.MovePosition(rb.transform.position + movement); } }
Food items are spawned by five spawn points. In order to make spawn time feel more “random”, only the first five items get spawned immediately at game start, and there’s no loop. The spawn points themselves will spawn a new item after the previously spawned one’s collider left its own “trigger collider zone”. This will also make sure foods don’t overlap.
Here’s the relevant code:
// Spawner parent object public class InitialSpawner : MonoBehaviour { public Spawner[] spawns; public FoodRepository repository; void Start () { foreach (Spawner spawn in spawns) { Spawn(spawn); } } public void Spawn(Spawner spawn) { StartCoroutine ("StartSpawn", spawn); } // 1st spawn private IEnumerator StartSpawn(Spawner spawn) { yield return new WaitForSeconds (Random.Range(0f, 0.1f)); Lean.Pool.LeanPool.Spawn (repository.PickFoodPrefab (), spawn.transform.position, spawn.transform.rotation); yield return null; } } // Each individual spawner object public class Spawner : MonoBehaviour { public InitialSpawner initialSpawner; void OnTriggerExit(Collider other) { Spawn (); } public void Spawn() { initialSpawner.Spawn (this); } }
All food items get destroyed by another trigger zone below the bottom of the screen.
For performance reasons, I’ve used a simple “object pooling” solution through which previously destroyed food items get recycled. I’ve used this great free solution which can be found in the Unity Asset store:
  • Lean Pool

Eating food

The player “eats up” food by picking it with the mouse. There’s a “pick while holding mouse button” logic to make this easier for the player:
// Attached to each food item public class CollectOnClick : MonoBehaviour { void OnMouseOver() { if (Input.GetMouseButton (0)) { CollectFood (); } } }
Now how do we count good food and reject the bad ones?
This logic is rather easy. Each food mode exists as an enum. A dictionary maps each food item to the modes to which it is compatible. When picking up food, this dictionary is consulted. Then, either the score is increased or the game over screen is shown (insta-death).
The same logic applies to the “food cleanup trigger”, only in reverse, as it detects good food that has been overlooked.

Individual food plan

In the “free style” mode, there’s neither scoring nor game over logic (except for a small “poisoning” easter egg). However, the “food picking” logic is reused to make a score-count per food mode. When the player exits the game through the “back” button, score percentage per food mode is calculated and shown.

Censoring

Let me share another fun fact about this app here: the version that’s available on Google’s app store is actually censored.
Originally, I planned to include foods like wine and beer. But I eventually figured this may trigger age restriction or any other kind of complications, so I removed the respecting code again prior to the public release of the app.

Other ingredients

There’s also some miscellaneous stuff I’ve used as part of this game:
  • particles hovering at the mouse position
  • scrolling background. Here’s a video tutorial on that (starting 19:07).
  • highscore saving. I may write an elaborate article about that at some later point.

Conclusion

This is my first “proper” game released for the Google Play app store. Even though it’s quite a simple game, it still covered various aspects of mobile game development. And it showed me once more how easy Unity makes it to realize a game idea.
I will continue my Unity journey, and I’m planning on writing more articles about stuff that I learn, some of which may be simple “behind the scenes” articles, others may discuss more specific game programming aspects in greater detail. Thank you for your interest.

Nicolas
Software Developer - Programmer
2
Game Languages
English
Supported Platforms
Android
Comments