Becoming a Fruit Ninja

Building the fruit ninja game in Virtual Reality

Mishaal Lakhani
5 min readApr 26, 2019

Let’s time travel one decade into the past. It’s 2009 and you’re rocking a graphic tee as you jam out to Party in the USA on your iPod. You reach into your lowrise jeans and pull out your iPhone 3G to play a game. Ahh, the classics! And your favourite, fruit ninja 🍉.

You reminiscing about the early smartphone days

Wouldn’t it be fun if instead of squinting to play on your itsy bitsy screen, you could become a fruit ninja yourself? Flash forward 10 years and this is actually possible!!!

🤯 How?

No, put your samurai sword away, we aren’t actually making a fruit salad. What I’m talking about is using virtual reality.

It would be so cool to have an immersive experience where you actually feel like you’re in that dojo and chopping up the fruit with your ninja skills. So I replicated a simplified version of the game in VR!!

Indeed, this is very crazy

This game is built for a 3dof headset such as google cardboard, which means the game uses where the player is looking as the “sword”.

Nice sword-y eyes

The goal is for the player to slash as many fruits as they can in 15 seconds.
Using the Unity game engine, Unity mono developer(in C#) and Blender(design engine), I replicated a kitchen dojo for the environment and fruits like bananas, watermelons, and apples.

When the player looks at a fruit and “cuts” it, the fruit disappears and points are added to the score.

How it works

The process of building this game isn’t as linear as it seems. We have to set up the 3D Primitives (basically simple shapes instead of details), add the code, go back to blender and create detailed artwork, overlay the artwork, and adjust the code. Let’s dive into how it works.

The environment

The main environment is where most of the process mentioned above happens.

We start off with basic versions of everything, kind of like sketching a drawing out first and then adding details later (these are the 3d primitives).

Basically the same process as the game artwork

This is to get rid of the technicalities and build up the game hierarchy.

The actual objects are stored within object classes like “fruit 1”

The positions in the game hierarchy are also structured so when the basic objects are overlayed, it’s easier to delete them and keep the mesh renderer

After building the environment and the objects within it, the interactions of these objects are created through the code.

The Code

The code has 3 parts to it(each with their own C# script integrated into unity): The game controller, the player and the fruits.

The Fruits

Different forces acting on the fruits of the objects including the horizontal and the vertical the rigid body and the spawning areas. The magnitude of all of these values is controlled through local variables.

Basically what the code looks like when applied to the fruit objects

If the fruit is hit (controlled in the player section below), it disappears. In the fruits script, it controls what happens if it isn’t hit and remains on the floor, in which case after a certain time span it disappears.

The player

The key component of this is raycast- raycasting is used to predict the player’s line of sight, so raycast.hit is when the player line of sight or focal point hits the fruit.

When the fruit is hit, 100 points are added to the player’s score and it disappears.

The game controller

The game controller contains variables for the boundaries, timings, and intervals of where the fruit is spawned (created), the game timer and the game text.

The fruit is instantiated randomly between the range that has been set, 3 pieces are created and they go to random height.

Since the text is dynamic, it corresponds to the text object created in the unity game developer. The text contains the player's score which is continuously added to, the instructions to chop the fruit/game over and the game timer.

What the info text variable basically does

The Art

Finally, there’s the art. Although it’s very basic, it takes quite a bit of time to make from scratch.

After using blender: 🎵You are 16 going on 70🎶

This is why the experience is more focused on the conceptually mimicking the game and the visuals are less of a focus in comparison to the code.

Overlaying: The 3d shapes peace out, the fruits come in

Once each individual fruit and the entire environment is made again in detail using blender, the original 3d primitives are overlayed.

Most of the original items are deleted or hidden, with the exception of the mesh filters (the area around the object where which is the target to hit).

And that's it! Not too bad for a pretty basic game huh?

Thank you for reading! If you enjoyed this article leave a few claps and follow my Medium and LinkedIn or check out my portfolio to follow my progress in VR and AI!

📝 Read this story later in Journal.

🗞 Wake up every Sunday morning to the week’s most noteworthy Tech stories, opinions, and news waiting in your inbox: Get the noteworthy newsletter >

--

--

Mishaal Lakhani

Modern day renaissance girl (in training). Learning things I find interesting about the world and life, and occasionally sharing them :)