Angry Teapots– using the physics engine in Unity Peter Passmore
What is Unity and what can I do with it? Unity is a fully integrated development engine that provides rich out-of-the-box functionality to create games and other interactive 3D content. You use Unity to assemble your art and assets into scenes and environments; add physics; simultaneously play test and edit your game, and when ready, publish to your chosen platforms, such as desktop computers, the Web, iOS, Android, Wii, PS3 and Xbox 360. Unity’s complete toolset, intuitive workspace and rapid, productive workflows help users to drastically reduce the time, effort and cost of making games.
Play some unity demos
Angry birds With a combined 1.7 billion downloads across all platforms and including both regular and special editions
Temple Run Temple Run 2 - Temple Run 2 Fastest Downloaded Mobile Game Ever, 50 Million Copies in Two Weeks (Thursday January 31, 2013) The previous fastest downloaded game was Angry Birds Space, selling 50 million copies in under six weeks bB3ZM bB3ZM
Critical Incident management training System using an Interactive Simulation environment PI: Prof William Wong, led by Middlesex University with 12 partners across 7 countries with ~€3.5M EU funding
Crisis
What do you need to make a 3D game?
3D objects – Geometry – Textures – Animation – Physics – Collision detection Lights Camera
Unity concepts Assets Scenes Game objects Components Scripts Prefabs
Unity Interface (windows|layouts|2 by 3)
Unity interface Scene – where game is constructed (set) Hierarchy – list of GameObjects used in the scene Inspector – settings for the currently selected asset/object e.g. its position scale Game – the preview window, where you actually play the game Project – a list of your assets (images, sounds etc)
The scene window The Hand tool – Pans the scene window. – If you hold down ALT it rotates the scene – If you hold down CTRL it zooms the scene The Translate tool – you can move you currently selected object along each of its axes The Rotate tool – rotate your object around each of the 3 axes The Scale tool - scale your object along each of the 3 axes – Drag on the central box to scale uniformly
Create two cubes Choose GameObject|Create Other|Cube to create a cube and set its postion and rotation as shown Choose GameObject|Create Other|Cube to create another cube, move it down Scale it to make a floor plane, name it Floor
Apply physics to the box Select cube Choose Components|Physics|RigidBody (note that gravity ticked in Inspector) Play animation (box falls and collides) Choose Assets|Import Package|Physic Materials click on All in the popup window Then select box1, in the Inspector under Box Collider click on small circle to the right next to Material – then Choose Bouncy When you play the game now the box bounces a lot
Note Bouncy in Box collider in the Inspector
Create a sphere Choose GameObject|Create Other|Sphere to create a sphere Make it a Rigid Body and Bouncy like the box Set its position manually by clicking on the sphere and typing in to the inspector setting its position
Light Choose GameObject|Create Other|Directional light Set its rotation as you like Move the view in the scene window until you can see everything well Select the Main Camera in the Hierarchy and choose GameObject|Align With View to use the same view for the game
Set shadows Select the Directional light and in the inspector turn on shadows by setting Shadow Type to Soft Shadows
E.g. I had this for Main Camera Transform With this scene view: See scene 1
Add colour (optional) Choose Assets|Create|Material A new material appears in the asset tab. Rename it by typing red Then in the Inspector window click on the white box next to main color, and a color picker box will appear, click on red
Add color (optional) Now drag the red material from the Assets tab and drop it on the sphere in the main window, the sphere now shows red Make two other colours for the floor and cube
Add texture – Basket ball Choose Assets|Import New Asset and navigate to basketBallTexture.jpg (in the assets folder) In the project window you will see the new texture as a Material Drag and drop this material onto the sphere to turn it into a basket ball
Add texture – cube and floor Now do the same for the cube using the texture called redPlasic.jpg Now do the same for the floor using the texture “Grass 02 seemless” In the inspector window set the tiling values for X and Y to 10 to get the texture down to a reasonable size.
Textured scene See scene3
Reset cube and sphere Transforms and view Reset cube transform to: Res t sphere transform to Change the view to so you can see the sphere and cube:
Write a script Right Click below the word Assets in the assets window and click on Create on the popup menu and then on Javascript A new document icon appears called New Behaviour, just type push and the document is now titles push Double click to edit the script After some time a window appears called mono editor
Edit the script so that it looks as follows:
Apply the script to the Sphere Save the script by choosing File|Save Now drag the push icon from the Assets window and drop it on the Sphere in the Hierarchy window The word sphere in the Hierarchy window will briefly show and blue line around Now if you click on the Sphere you will see that the script is attached to it at the bottom of the Inspector window
Script Click on the sphere and in the inspector window in crease the vale of powerZ until the ball hits the cube (e.g. 850) See Scene4
Collision! Now when you run Unity you will see that the – force is immediately applied to the sphere – and that it collides with the box and – both the box and the sphere behave like physical objects according to Newtons laws of motion. Click on the sphere in the Hierarchy window and then change the value of Powerz in the push Script section of the Inspector window to Explain what happens when you run the program.
Get a teapot In this case I import teapot from a 3D modelling package call 3D studio MAX You can find many 3d models on the net ALL you do is choose Assets|Import New Asset Then you new asset will appear in the assets tab And you can drag and drop it into the scene
Make the teapot angry!
Put the teapot in the scene Choose Assets|Import New Asset to import a the teapot into the scene. Set its transform to place it on the cube: Select the sphere and in the inspector set the value of PowerZ to 900 so the ball hits the teapot See scene 5
Add Skybox Choose Assets|ImportPackage|Skyboxes then click import in the popup window Choose Edit |Render Settings Next to Skybox Material in the Inspector tab on the right there is a small circle with a dot in the middle. Click this and it brings up the Select Material window. Choose Sunny 1 you now have a semi cloudy sky
Ball and teapot collide See scene 6
Step up time - optional Choose Edit|Project Settings |Time and change Time Scale eg from 1 to e.g. 4 You may have to change the values of forces after this (eg in the push script)
Make two mesh collider objects collide Select the teapot in the Hierarchy window Right click and chose duplicate and a copy is made in the exact same position as the existing one. Move it up to see it. If you run the program the two teapot pass through each other To fix this problem tick the Convex flag in the mesh collider section of either teapot (this is supposed to only work for object of 256 or less polygons)
All these steps are in the tutorial project on your computer Now build your own game/simulation!