Last year Unity’s 3D engine was made free in it’s basic form. And since 3D graphics is fun, and 3D graphics made easy is even more fun, of course it had to be tried out.
And Unity really has made it easy to create 3D applications. One of the biggest strengths of Unity is that the user can create his project once and then publish it to several platforms, such as Mac OS X, Windows or Unity’s very own webplayer. With Unity Pro the applications can even be published to the Nintendo Wii or iPhone/iPad.
Additionally the editor itself is easy to use. Objects in the world, suchs as geometry or lightsources, can be created through the GameObject abstraction, and adding physics or scriptet behaviour to them is no further than a click away.
A fun starter project, which combines geometric figures with physics and scripting, is creating xkcd’s Hell. Aside from easy to create geometry, the point system is trivially implemented.
First the blocks will have to be made. In the menu choose GameObject -> CreateOther and then click on Cube, which creates a cube in the world. These cubes can then be scaled and translated to create all the familiar tetris blocks.
To be able to work with the blocks as a single piece of geometry, the hierarchical nature of the scene graph can be used. An empty GameObject will be created for each block and then that blocks geometry will be dragged unto the GameObject. This will allow us to add gravity and collision to all the cubes that make up a block, simply by adding a rigidbody komponent to their parent GameObject.
The final thing that needs to be done to the geometry is to place it somewhere where we can instantiate new clones of the original blocks. Here Unity’s prefabs, prefabricated geometry, comes in handy. If we wrap our geometry in a prefab, creating a new instance is then simply done by dragging the prefab into the scene or through scripted instantiation.
The geometry of the level itself is made with 7 scaled, translated and rotated cubes.
The logic that combines the geometry into Tetris is then created by adding 3 scripts.
The second script is StartGame.js. This script sole purpose is to ask the almighty god of tetris to start the game by creating the first block. The script is added to the camera GameObject and will execute when the camera ‘awakes’.
The last script is called MoveBlocks.js and is added to all the blocks. MoveBlocks handles player input that moves the blocks, making sure the blocks don’t simply fall off the level, ie always has a z position set to 0, and that a new block is spawned once the current one collides with another object. Apart from that the script attempts to remove blocks when they are no longer visible by the camera.
The result can be seen here. A little extra touch of agony and torment is added by not testing to see if there is room for spawning a new block. A block can therefore spawn right on top of another, resulting in a rain blocks and a player in distress.
To find out more about Unity and how to get started doing 3D projects, check out Unity.com’s resource section or go to youtube to watch one of the many Unity tutorials available.