Behind VR Games: Fruit Ninja
I’m not sure why, but slicing virtual fruit on my phone is extremely addicting. I used to love playing the Fruit Ninja app back when it came out and would spend hours trying to beat my friends’ high scores. As a result, when I started looking into virtual reality and learning about this awesome technology where you can create interactive 3D environments and games, I decided that I would make a spin-off of my favourite app in VR.
This project was built for the Google Cardboard in the Unity Game Engine and has the simple goal of making players slash as many fruit as possible within 15 seconds. The setting takes place within a dojo, where players can see watermelons, bananas, and apples being thrown into the air in front of them, as shown above. After each fruit is cut, it disappears and 100 points is added to the total score, which can be viewed on-screen for the entire game. If a fruit falls to the ground without being cut, it destroys itself, no points are gained, and the gameplay continues. To prevent cheating, the player is unable to move around and can only cut the fruit by looking at it with their field of vision.
How the Game Works:
- Throwing the Fruit
To guarantee that the fruits don’t appear in random locations, it is important to set parameters as to how they should behave in the game. The variables verticalForce and horizontalForce are what allow for the fruit to be thrown into the air, while the middle section of the code, referring to Rigidbody, allows for the fruit to experience the properties of physics. Thanks to this, all the fruit must fall down due to gravity, no matter what direction it is thrown in. Furthermore, by giving verticalForce and horizontalForce random values within a specified range, the fruits are able to spawn and move in different, random directions to make the game more unpredictable.Lastly, the lifetime of the fruit refers to how long it appears on screen, and the code above ensures that each fruit is deleted after 2 seconds.
2. Controlling the Game
The second part of the program deals with the code necessary to ensure that the game can run properly. Here we can find the variables gameTimer and resetTimer which control the duration of the game (15 seconds in this case) and how long it takes before it restarts. Moreover, the variables spawnDuration and spawnTimer control how long it takes before each fruit is shown on screen. This code also ensures that there are 3 fruit spawned each time and that they are spawned randomly within a specific area of the screen.
3. Arranging In-game Text
In order to ensure that the text in-game can be updated real-time with the score and amount of seconds left, there needs to be variables that keep track of the player’s total points collected and how long they have been playing for. The code above shows the messages that can be seen while playing the game, and the different variables (gameTimer and player.score) which allow for the text to be updated as time goes on.
4. Slashing Fruits
Last but not least, the game can’t be Fruit Ninja without having the ability to slice fruits in half. This final part of the program is what detects the field of vision of the user, with the help of the Raycast, and recognizes when it lands on one of the fruits. For those of you who don’t know, a raycast is a ray that gets sent out from a position in 3D or 2D space and moves in a specific direction. In this case, the ray acts as the field of vision of the player and is what allows for the destruction of the fruits.
Once the ray comes in contact with a fruit, the score is increased and the fruit (or gameObject as it is called in the code) is destroyed. For example, if you were to look directly at an apple, it would immediately disappear and 100 points would be added to your total score.
For the visual aspect and design of the game, I used a software called Blender, which is an open-source 3D computer graphics software toolset used for creating animated films, visual effects, art, 3D printed models, interactive 3D applications and video games. With this tool I was able to create the design of the fruits and the background of the game, just like the banana pictured above. Because this was my first time using this software, each design took me a very long time to create and thus I wasn’t able to include as much detail into the background of the dojo where the game takes place. That being said, it is a skill which I plan on developing more and more so there will definitely be visible improvement in my next VR environment.
Overall, my version of this game definitely did not reach the same level of complexity and visual design as the Fruit Ninja app. That being said, I think it encapsulated the overall theme of the game fairly well. This is one of the first few games which I have built in VR so I can’t wait to see how I will improve as time goes on!
Thanks for reading and be sure to look for more articles coming your way on other VR projects I’ve been working on! To learn more about the game and see what the gameplay looks like, watch the video below:
If you enjoyed this article, please give it some claps so I know you liked it! You can also follow me on LinkedIn to see what I’m up to or check out my personal website to learn more about other projects I’ve completed in the past!
Finally, if you have any constructive criticism or feedback, be sure to leave a comment below! I’d love to learn ways to improve my articles/writing style :)