Unity3D - Computing Systemsbranko-cirovic.appspot.com/CP3830/Unity3D.Basics.pdf · Unity3D Unity3D...

121
Unity3D Unity3D is a powerful cross-platform 3D engine and a user friendly development environment. If you didn’t like OpenGL, hopefully you’ll like this. Remember the Rotating Earth? Look how it’s done with Unity3D.

Transcript of Unity3D - Computing Systemsbranko-cirovic.appspot.com/CP3830/Unity3D.Basics.pdf · Unity3D Unity3D...

Page 1: Unity3D - Computing Systemsbranko-cirovic.appspot.com/CP3830/Unity3D.Basics.pdf · Unity3D Unity3D is a powerful cross-platform 3D engine ... select Main Camera. ... (Maya, Sketchup

Unity3D

Unity3D is a powerful cross-platform 3D engine and a user friendly development environment.

If you didn’t like OpenGL, hopefully you’ll like this.

Remember the Rotating Earth? Look how it’s done with Unity3D.

Page 2: Unity3D - Computing Systemsbranko-cirovic.appspot.com/CP3830/Unity3D.Basics.pdf · Unity3D Unity3D is a powerful cross-platform 3D engine ... select Main Camera. ... (Maya, Sketchup

Rotating EarthFire up Unity3D

Page 3: Unity3D - Computing Systemsbranko-cirovic.appspot.com/CP3830/Unity3D.Basics.pdf · Unity3D Unity3D is a powerful cross-platform 3D engine ... select Main Camera. ... (Maya, Sketchup

Rotating EarthCreate new project (“Rotating Earth”):

Page 4: Unity3D - Computing Systemsbranko-cirovic.appspot.com/CP3830/Unity3D.Basics.pdf · Unity3D Unity3D is a powerful cross-platform 3D engine ... select Main Camera. ... (Maya, Sketchup

Rotating EarthFrom the top menu add Game Object > 3D Object > Sphere

Page 5: Unity3D - Computing Systemsbranko-cirovic.appspot.com/CP3830/Unity3D.Basics.pdf · Unity3D Unity3D is a powerful cross-platform 3D engine ... select Main Camera. ... (Maya, Sketchup

Rotating EarthWith sphere selected, change its scale to 8, 8, 8 in the Inspector:

Page 6: Unity3D - Computing Systemsbranko-cirovic.appspot.com/CP3830/Unity3D.Basics.pdf · Unity3D Unity3D is a powerful cross-platform 3D engine ... select Main Camera. ... (Maya, Sketchup

Rotating Earth

Download earth.png and drag it from Finder into Assets folder at the bottom:

Page 7: Unity3D - Computing Systemsbranko-cirovic.appspot.com/CP3830/Unity3D.Basics.pdf · Unity3D Unity3D is a powerful cross-platform 3D engine ... select Main Camera. ... (Maya, Sketchup

Rotating EarthNow, drag earth.png from Assets folder onto the sphere and release:

Observe that new subfolder Materials has been created with material earth in it:

Page 8: Unity3D - Computing Systemsbranko-cirovic.appspot.com/CP3830/Unity3D.Basics.pdf · Unity3D Unity3D is a powerful cross-platform 3D engine ... select Main Camera. ... (Maya, Sketchup

Rotating EarthHit Play button on top:

Page 9: Unity3D - Computing Systemsbranko-cirovic.appspot.com/CP3830/Unity3D.Basics.pdf · Unity3D Unity3D is a powerful cross-platform 3D engine ... select Main Camera. ... (Maya, Sketchup

Rotating Earth

Let’s change the background. From Hierarchy (upper left corner) select Main Camera.

In Inspector (upper right corner), change Clear Flags from Skybox to Solid color and change background to black (0, 0, 0)

Page 10: Unity3D - Computing Systemsbranko-cirovic.appspot.com/CP3830/Unity3D.Basics.pdf · Unity3D Unity3D is a powerful cross-platform 3D engine ... select Main Camera. ... (Maya, Sketchup

Rotating EarthHit Play button on top:

Page 11: Unity3D - Computing Systemsbranko-cirovic.appspot.com/CP3830/Unity3D.Basics.pdf · Unity3D Unity3D is a powerful cross-platform 3D engine ... select Main Camera. ... (Maya, Sketchup

Rotating EarthIn order to rotate earth about its own axis we need a script.

Select Sphere from Hierarchy and in Inspector Add Component > Scripts > New Script (rename to RotateEarth), Language C#

Observe that newly created script appears in Assets

Page 12: Unity3D - Computing Systemsbranko-cirovic.appspot.com/CP3830/Unity3D.Basics.pdf · Unity3D Unity3D is a powerful cross-platform 3D engine ... select Main Camera. ... (Maya, Sketchup

RotateEarth.csDouble-click on script in Assets and MonoDevelop editor should appear.

Script comes with two empty methods:

void Start() which is used for variable initialization, and

void Update() which is called once per frame.

Page 13: Unity3D - Computing Systemsbranko-cirovic.appspot.com/CP3830/Unity3D.Basics.pdf · Unity3D Unity3D is a powerful cross-platform 3D engine ... select Main Camera. ... (Maya, Sketchup

RotateEarth.csusing UnityEngine; using System.Collections;

public class RotateEarth : MonoBehaviour {

// Use this for initialization void Start () { } // Update is called once per frame void Update () { transform.Rotate(Vector3.up, 20 * Time.deltaTime); } }

Page 14: Unity3D - Computing Systemsbranko-cirovic.appspot.com/CP3830/Unity3D.Basics.pdf · Unity3D Unity3D is a powerful cross-platform 3D engine ... select Main Camera. ... (Maya, Sketchup

RotateEarth.cs

Function Rotate() takes two arguments:

Vector3.up - Specifies rotation axis (0, 1, 0)

Time.deltaTime - The time in seconds it took to complete the last frame (angle argument).

Page 15: Unity3D - Computing Systemsbranko-cirovic.appspot.com/CP3830/Unity3D.Basics.pdf · Unity3D Unity3D is a powerful cross-platform 3D engine ... select Main Camera. ... (Maya, Sketchup

RotateEarth.csIf we wanted to rotate Earth about its axis AND about the origin (0, 1, 0) we need another function:

public void RotateAround(Vector3 point, Vector3 axis, float angle);

which rotates the transform about axis passing through point in world coordinates by angle degrees.

Page 16: Unity3D - Computing Systemsbranko-cirovic.appspot.com/CP3830/Unity3D.Basics.pdf · Unity3D Unity3D is a powerful cross-platform 3D engine ... select Main Camera. ... (Maya, Sketchup

RotateEarth.csusing UnityEngine; using System.Collections;

public class RotateEarth : MonoBehaviour {

// Use this for initialization void Start () { } // Update is called once per frame void Update () { transform.RotateAround (Vector3.zero, Vector3.up, 14 * Time.deltaTime); transform.Rotate(Vector3.up, 20 * Time.deltaTime); } }

Page 17: Unity3D - Computing Systemsbranko-cirovic.appspot.com/CP3830/Unity3D.Basics.pdf · Unity3D Unity3D is a powerful cross-platform 3D engine ... select Main Camera. ... (Maya, Sketchup

RotateEarth.cs

If you play it now, there will be no difference, since both vectors coincide.

Translate Sphere along the z axis (z = 4) and run.

Page 18: Unity3D - Computing Systemsbranko-cirovic.appspot.com/CP3830/Unity3D.Basics.pdf · Unity3D Unity3D is a powerful cross-platform 3D engine ... select Main Camera. ... (Maya, Sketchup

It’s alive!

Page 19: Unity3D - Computing Systemsbranko-cirovic.appspot.com/CP3830/Unity3D.Basics.pdf · Unity3D Unity3D is a powerful cross-platform 3D engine ... select Main Camera. ... (Maya, Sketchup

Unity Interface

Inspector

SceneHierarchy

Project

Toolbar

Page 20: Unity3D - Computing Systemsbranko-cirovic.appspot.com/CP3830/Unity3D.Basics.pdf · Unity3D Unity3D is a powerful cross-platform 3D engine ... select Main Camera. ... (Maya, Sketchup

Scene NavigationArrow Movement

The up and down arrows move the camera forward and backward in the direction it is facing.

The left and right arrows pan the view sideways.

Hold down the Shift key with an arrow to move faster.

Page 21: Unity3D - Computing Systemsbranko-cirovic.appspot.com/CP3830/Unity3D.Basics.pdf · Unity3D Unity3D is a powerful cross-platform 3D engine ... select Main Camera. ... (Maya, Sketchup

Scene NavigationFocusing

If you select a GameObject in the hierarchy, then move the mouse over the scene view and press F, the view will move so as to center on the object.

This feature is referred to as ‘Frame Selected’ under the ‘Edit’ menu.

Page 22: Unity3D - Computing Systemsbranko-cirovic.appspot.com/CP3830/Unity3D.Basics.pdf · Unity3D Unity3D is a powerful cross-platform 3D engine ... select Main Camera. ... (Maya, Sketchup

Scene Navigation

Move, Orbit and Zoom

When the hand tool is selected (shortcut: Q), the following mouse controls are available:

Move: Click-drag to drag the camera around.

Page 23: Unity3D - Computing Systemsbranko-cirovic.appspot.com/CP3830/Unity3D.Basics.pdf · Unity3D Unity3D is a powerful cross-platform 3D engine ... select Main Camera. ... (Maya, Sketchup

Scene NavigationOrbit: Hold Alt and click-drag to orbit the camera around the current pivot point. This option is not available in 2D mode as the view is orthographic.

Zoom: Hold Alt and right click-drag to zoom the Scene View. On Mac you can also hold Control and click-drag instead.

Page 24: Unity3D - Computing Systemsbranko-cirovic.appspot.com/CP3830/Unity3D.Basics.pdf · Unity3D Unity3D is a powerful cross-platform 3D engine ... select Main Camera. ... (Maya, Sketchup

Scene NavigationFlythrough Mode mode lets you navigate the Scene View by flying around in first person view.

Click and hold the right mouse button.

Use mouse and WASD keys to move left/right forward/backward and QE keys to move up and down.

Holding down Shift will make you move faster.

Page 25: Unity3D - Computing Systemsbranko-cirovic.appspot.com/CP3830/Unity3D.Basics.pdf · Unity3D Unity3D is a powerful cross-platform 3D engine ... select Main Camera. ... (Maya, Sketchup

Primitive ObjectsUnity can work with 3D models of any shape that can be created with modelling software.

There are also a number of primitive object types that can be created directly within Unity, namely the Cube, Sphere, Capsule, Cylinder, Plane and Quad.

Any of the primitives can be added to the scene using the appropriate item on the GameObject > 3D Object menu.

Page 26: Unity3D - Computing Systemsbranko-cirovic.appspot.com/CP3830/Unity3D.Basics.pdf · Unity3D Unity3D is a powerful cross-platform 3D engine ... select Main Camera. ... (Maya, Sketchup

Cube

This is a simple cube with sides one unit long, textured so that the image is repeated on each of the six faces.

Page 27: Unity3D - Computing Systemsbranko-cirovic.appspot.com/CP3830/Unity3D.Basics.pdf · Unity3D Unity3D is a powerful cross-platform 3D engine ... select Main Camera. ... (Maya, Sketchup

Sphere

This is a sphere of unit diameter, textured so that the entire image wraps around once with the top and bottom “pinched” at the poles.

Page 28: Unity3D - Computing Systemsbranko-cirovic.appspot.com/CP3830/Unity3D.Basics.pdf · Unity3D Unity3D is a powerful cross-platform 3D engine ... select Main Camera. ... (Maya, Sketchup

Capsule

A capsule is a cylinder with hemispherical caps at the ends. The object is one unit in diameter and two units high.

Page 29: Unity3D - Computing Systemsbranko-cirovic.appspot.com/CP3830/Unity3D.Basics.pdf · Unity3D Unity3D is a powerful cross-platform 3D engine ... select Main Camera. ... (Maya, Sketchup

Cylinder

This is a simple cylinder which is two units high and one unit in diameter.

Page 30: Unity3D - Computing Systemsbranko-cirovic.appspot.com/CP3830/Unity3D.Basics.pdf · Unity3D Unity3D is a powerful cross-platform 3D engine ... select Main Camera. ... (Maya, Sketchup

Plane

This is a flat square with edges ten units long oriented in the XZ plane of the local coordinate space.

Page 31: Unity3D - Computing Systemsbranko-cirovic.appspot.com/CP3830/Unity3D.Basics.pdf · Unity3D Unity3D is a powerful cross-platform 3D engine ... select Main Camera. ... (Maya, Sketchup

Quad

The quad primitive resembles the plane but its edges are only one unit long and the surface is oriented in the XY plane.

Page 32: Unity3D - Computing Systemsbranko-cirovic.appspot.com/CP3830/Unity3D.Basics.pdf · Unity3D Unity3D is a powerful cross-platform 3D engine ... select Main Camera. ... (Maya, Sketchup

Complex objectsThere are several different ways to include complex objects into the scene:

1. By importing objects from Unity Asset Store

2. By importing objects designed in some modelling software (Maya, Sketchup etc)

3. By using Probuilder

Page 33: Unity3D - Computing Systemsbranko-cirovic.appspot.com/CP3830/Unity3D.Basics.pdf · Unity3D Unity3D is a powerful cross-platform 3D engine ... select Main Camera. ... (Maya, Sketchup

Probuilder Basic

Page 34: Unity3D - Computing Systemsbranko-cirovic.appspot.com/CP3830/Unity3D.Basics.pdf · Unity3D Unity3D is a powerful cross-platform 3D engine ... select Main Camera. ... (Maya, Sketchup

MaterialsWe have already used materials in our Rotating Earth project.

They play an essential part in defining how our object is displayed.

Page 35: Unity3D - Computing Systemsbranko-cirovic.appspot.com/CP3830/Unity3D.Basics.pdf · Unity3D Unity3D is a powerful cross-platform 3D engine ... select Main Camera. ... (Maya, Sketchup

CreatingMaterialsTo create a new Material, use Assets > Create > Material from the main menu or the Project View context menu.

By default, new materials are assigned the Standard Shader, with all map properties empty.

Page 36: Unity3D - Computing Systemsbranko-cirovic.appspot.com/CP3830/Unity3D.Basics.pdf · Unity3D Unity3D is a powerful cross-platform 3D engine ... select Main Camera. ... (Maya, Sketchup

Using Materials

Once the Material has been created, you can apply it to an object and tweak all of its properties in the Inspector.

To apply it to an object, just drag it from the Project View to any object in the Scene or Hierarchy.

Page 37: Unity3D - Computing Systemsbranko-cirovic.appspot.com/CP3830/Unity3D.Basics.pdf · Unity3D Unity3D is a powerful cross-platform 3D engine ... select Main Camera. ... (Maya, Sketchup

Material Properties

The properties that a Material’s inspector displays are determined by the Shader that the Material uses.

A shader is a specialised kind of graphical program that determines how texture and lighting information are combined to generate the pixels of the rendered object onscreen.

Page 38: Unity3D - Computing Systemsbranko-cirovic.appspot.com/CP3830/Unity3D.Basics.pdf · Unity3D Unity3D is a powerful cross-platform 3D engine ... select Main Camera. ... (Maya, Sketchup

Material Properties

You can select which Shader you want any particular Material to use.

Simply expand the Shader drop-down in the Inspector, and choose your new Shader.

The Shader you choose will dictate the available properties to change.

Page 39: Unity3D - Computing Systemsbranko-cirovic.appspot.com/CP3830/Unity3D.Basics.pdf · Unity3D Unity3D is a powerful cross-platform 3D engine ... select Main Camera. ... (Maya, Sketchup

Material Properties

The properties can be colors, sliders, textures, numbers, or vectors.

If you have applied the Material to an active object in the Scene, you will see your property changes applied to the object in real-time.

Page 40: Unity3D - Computing Systemsbranko-cirovic.appspot.com/CP3830/Unity3D.Basics.pdf · Unity3D Unity3D is a powerful cross-platform 3D engine ... select Main Camera. ... (Maya, Sketchup

Material Properties

There are two ways to apply a Texture to a property.

1. Drag it from the Project View on top of the Texture square

2. Click the Select button, and choose the texture from the drop-down list that appears

Page 41: Unity3D - Computing Systemsbranko-cirovic.appspot.com/CP3830/Unity3D.Basics.pdf · Unity3D Unity3D is a powerful cross-platform 3D engine ... select Main Camera. ... (Maya, Sketchup

Built-in Shaders

FX Lighting and glass effects

GUI and UI For user interface graphics

Mobile Simplified high-performance shader for mobile devices

Nature For trees and terrain

Particles Particle system effects

Page 42: Unity3D - Computing Systemsbranko-cirovic.appspot.com/CP3830/Unity3D.Basics.pdf · Unity3D Unity3D is a powerful cross-platform 3D engine ... select Main Camera. ... (Maya, Sketchup

Built-in Shaders

Skybox For rendering background environments behind all geometry

Sprites For use with the 2D sprite system

Toon Cartoon-style rendering

Unlit For rendering that entirely bypasses all light & shadowing

Legacy The large collection of older shaders which were superseded by the Standard Shader

Page 43: Unity3D - Computing Systemsbranko-cirovic.appspot.com/CP3830/Unity3D.Basics.pdf · Unity3D Unity3D is a powerful cross-platform 3D engine ... select Main Camera. ... (Maya, Sketchup

Lighting

Light source

Bright shading

Dark shading

In order to calculate the shading of a 3D object, Unity needs to know the intensity, direction and

color of the light.

These properties are provided by Light

objects in the scene.

Page 44: Unity3D - Computing Systemsbranko-cirovic.appspot.com/CP3830/Unity3D.Basics.pdf · Unity3D Unity3D is a powerful cross-platform 3D engine ... select Main Camera. ... (Maya, Sketchup

Types of lightPoint Light is located at a point in space and sends light out in all directions equally.

Page 45: Unity3D - Computing Systemsbranko-cirovic.appspot.com/CP3830/Unity3D.Basics.pdf · Unity3D Unity3D is a powerful cross-platform 3D engine ... select Main Camera. ... (Maya, Sketchup

Point LightUseful for simulating lamps and other local sources of light in a scene.

Page 46: Unity3D - Computing Systemsbranko-cirovic.appspot.com/CP3830/Unity3D.Basics.pdf · Unity3D Unity3D is a powerful cross-platform 3D engine ... select Main Camera. ... (Maya, Sketchup

Types of LightSpot Light has a specified location and range over which the light falls off.

Page 47: Unity3D - Computing Systemsbranko-cirovic.appspot.com/CP3830/Unity3D.Basics.pdf · Unity3D Unity3D is a powerful cross-platform 3D engine ... select Main Camera. ... (Maya, Sketchup

Spot LightSpot lights are generally used for artificial light sources such as flashlights

Page 48: Unity3D - Computing Systemsbranko-cirovic.appspot.com/CP3830/Unity3D.Basics.pdf · Unity3D Unity3D is a powerful cross-platform 3D engine ... select Main Camera. ... (Maya, Sketchup

Types of LightDirectional Light represents large, distant source that comes from a position outside the range of the game world.

Direction only

Page 49: Unity3D - Computing Systemsbranko-cirovic.appspot.com/CP3830/Unity3D.Basics.pdf · Unity3D Unity3D is a powerful cross-platform 3D engine ... select Main Camera. ... (Maya, Sketchup

Directional LightCan be used to simulate the sun or moon

Page 50: Unity3D - Computing Systemsbranko-cirovic.appspot.com/CP3830/Unity3D.Basics.pdf · Unity3D Unity3D is a powerful cross-platform 3D engine ... select Main Camera. ... (Maya, Sketchup

Types of LightArea Light is defined by a rectangle in space. Light is emitted in all directions, but only from one side of the rectangle. The light falls off over a specified range.

range Can be used to create a realistic street light for example.

Page 51: Unity3D - Computing Systemsbranko-cirovic.appspot.com/CP3830/Unity3D.Basics.pdf · Unity3D Unity3D is a powerful cross-platform 3D engine ... select Main Camera. ... (Maya, Sketchup

Using lightsGameObject > Light > Type

Page 52: Unity3D - Computing Systemsbranko-cirovic.appspot.com/CP3830/Unity3D.Basics.pdf · Unity3D Unity3D is a powerful cross-platform 3D engine ... select Main Camera. ... (Maya, Sketchup

Shadows

Illuminated area

Shadow where light doesn’t reach the surface

Page 53: Unity3D - Computing Systemsbranko-cirovic.appspot.com/CP3830/Unity3D.Basics.pdf · Unity3D Unity3D is a powerful cross-platform 3D engine ... select Main Camera. ... (Maya, Sketchup

ShadowsShadows for an individual light are set with the Shadow Type property.

Objects have properties Cast and Receive Shadows.

Page 54: Unity3D - Computing Systemsbranko-cirovic.appspot.com/CP3830/Unity3D.Basics.pdf · Unity3D Unity3D is a powerful cross-platform 3D engine ... select Main Camera. ... (Maya, Sketchup

Cookies

Shadows can also be created by placing a shaped mask in between the light source and the action.

The mask is known as a cucoloris or cookie

A cookie is just an ordinary texture but only the alpha/transparency channel is relevant

Page 55: Unity3D - Computing Systemsbranko-cirovic.appspot.com/CP3830/Unity3D.Basics.pdf · Unity3D Unity3D is a powerful cross-platform 3D engine ... select Main Camera. ... (Maya, Sketchup

CookiesSimple cookie for a window light

Imported into assets

Inspector settings Texture Type: Cookie

Page 56: Unity3D - Computing Systemsbranko-cirovic.appspot.com/CP3830/Unity3D.Basics.pdf · Unity3D Unity3D is a powerful cross-platform 3D engine ... select Main Camera. ... (Maya, Sketchup

Cookies

In order to apply a cookie to a light, select light and drag it to the Light’s Cookie property in the inspector to apply it.

Page 57: Unity3D - Computing Systemsbranko-cirovic.appspot.com/CP3830/Unity3D.Basics.pdf · Unity3D Unity3D is a powerful cross-platform 3D engine ... select Main Camera. ... (Maya, Sketchup

Cookies

Page 58: Unity3D - Computing Systemsbranko-cirovic.appspot.com/CP3830/Unity3D.Basics.pdf · Unity3D Unity3D is a powerful cross-platform 3D engine ... select Main Camera. ... (Maya, Sketchup

Roll a BallCreate new Project

Page 59: Unity3D - Computing Systemsbranko-cirovic.appspot.com/CP3830/Unity3D.Basics.pdf · Unity3D Unity3D is a powerful cross-platform 3D engine ... select Main Camera. ... (Maya, Sketchup

Scene

Save scene (File, Save Scene as or ⌘S)

Page 60: Unity3D - Computing Systemsbranko-cirovic.appspot.com/CP3830/Unity3D.Basics.pdf · Unity3D Unity3D is a powerful cross-platform 3D engine ... select Main Camera. ... (Maya, Sketchup

Add Object to SceneAdd Plane to the scene (Game Object, 3D, Plane)

Page 61: Unity3D - Computing Systemsbranko-cirovic.appspot.com/CP3830/Unity3D.Basics.pdf · Unity3D Unity3D is a powerful cross-platform 3D engine ... select Main Camera. ... (Maya, Sketchup

PositionIn Hierarchy select Plane and in Inspector, change its name to Ground

If the Ground is not positioned in the center of the coordinate system (0, 0, 0), reset it using “gear”

Page 62: Unity3D - Computing Systemsbranko-cirovic.appspot.com/CP3830/Unity3D.Basics.pdf · Unity3D Unity3D is a powerful cross-platform 3D engine ... select Main Camera. ... (Maya, Sketchup

Scaling

Plane can be scaled either by selecting the “scale” icon and stretching the plane in X or Z direction

Or directly setting transformation scale coordinates in the Inspector (2, 1, 2)

Page 63: Unity3D - Computing Systemsbranko-cirovic.appspot.com/CP3830/Unity3D.Basics.pdf · Unity3D Unity3D is a powerful cross-platform 3D engine ... select Main Camera. ... (Maya, Sketchup

More ObjectsAdd a player (Sphere from object menu) and rename it to Player. Observe that Sphere is “buried” in the plane.

Page 64: Unity3D - Computing Systemsbranko-cirovic.appspot.com/CP3830/Unity3D.Basics.pdf · Unity3D Unity3D is a powerful cross-platform 3D engine ... select Main Camera. ... (Maya, Sketchup

Change PositionCenter of the sphere is in the centre of the coordinate system. In Inspector, set sphere’s Y position coordinate to 0.5

Page 65: Unity3D - Computing Systemsbranko-cirovic.appspot.com/CP3830/Unity3D.Basics.pdf · Unity3D Unity3D is a powerful cross-platform 3D engine ... select Main Camera. ... (Maya, Sketchup

Materials and ColorsCreate new Folder under Project/Create. Rename it to Materials.

Page 66: Unity3D - Computing Systemsbranko-cirovic.appspot.com/CP3830/Unity3D.Basics.pdf · Unity3D Unity3D is a powerful cross-platform 3D engine ... select Main Camera. ... (Maya, Sketchup

Materials and ColorsWith Materials folder selected, create Material. Rename “New Material” to Background.

Page 67: Unity3D - Computing Systemsbranko-cirovic.appspot.com/CP3830/Unity3D.Basics.pdf · Unity3D Unity3D is a powerful cross-platform 3D engine ... select Main Camera. ... (Maya, Sketchup

Materials and ColorsWith Material selected, in Inspector, next to Albedo is the color selector. Set it to some color (e.g. 0, 32, 64)

Page 68: Unity3D - Computing Systemsbranko-cirovic.appspot.com/CP3830/Unity3D.Basics.pdf · Unity3D Unity3D is a powerful cross-platform 3D engine ... select Main Camera. ... (Maya, Sketchup

Apply Material to ObjectTo apply material to an object, simply select material and drag into object (Ground in our case).

Page 69: Unity3D - Computing Systemsbranko-cirovic.appspot.com/CP3830/Unity3D.Basics.pdf · Unity3D Unity3D is a powerful cross-platform 3D engine ... select Main Camera. ... (Maya, Sketchup

Change Light DirectionSelect Directional Light and using Inspector, change Rotation Y coordinate to 60

Page 70: Unity3D - Computing Systemsbranko-cirovic.appspot.com/CP3830/Unity3D.Basics.pdf · Unity3D Unity3D is a powerful cross-platform 3D engine ... select Main Camera. ... (Maya, Sketchup

Rigid BodiesHow to move Player around the Ground?

This Requires Physics. Select Player (Sphere) and in Inspector, Add Component, Physics, Rigid body

Page 71: Unity3D - Computing Systemsbranko-cirovic.appspot.com/CP3830/Unity3D.Basics.pdf · Unity3D Unity3D is a powerful cross-platform 3D engine ... select Main Camera. ... (Maya, Sketchup

Rigidbody

Rigidbodies enable GameObjects to act under the control of physics.

The Rigidbody can receive forces and torque to make objects move in a realistic way.

Page 72: Unity3D - Computing Systemsbranko-cirovic.appspot.com/CP3830/Unity3D.Basics.pdf · Unity3D Unity3D is a powerful cross-platform 3D engine ... select Main Camera. ... (Maya, Sketchup

Rigidbody properties

The mass of the object (in kilograms by default).Mass

How much air resistance affects the object when moving from forces. 0 means no air resistance, and infinity makes the object stop moving immediately.

Drag

Page 73: Unity3D - Computing Systemsbranko-cirovic.appspot.com/CP3830/Unity3D.Basics.pdf · Unity3D Unity3D is a powerful cross-platform 3D engine ... select Main Camera. ... (Maya, Sketchup

Rigidbody properties

How much air resistance affects the object when rotating from torque. 0 means no air resistance.

Angular Drag

If enabled, the object is affected by gravity.Use Gravity

Page 74: Unity3D - Computing Systemsbranko-cirovic.appspot.com/CP3830/Unity3D.Basics.pdf · Unity3D Unity3D is a powerful cross-platform 3D engine ... select Main Camera. ... (Maya, Sketchup

Rigidbody properties

If enabled, the object will not be driven by the physics engine, and can only be manipulated by its Transform.

Is Kinematic

Try one of the options only if you are seeing jerkiness in your Rigidbody’s movement.

Interpolate

Page 75: Unity3D - Computing Systemsbranko-cirovic.appspot.com/CP3830/Unity3D.Basics.pdf · Unity3D Unity3D is a powerful cross-platform 3D engine ... select Main Camera. ... (Maya, Sketchup

Rigidbody properties

Used to prevent fast moving objects from passing through other objects without detecting collisions.

Collision Detection

Restrictions on the Rigidbody’s motion.Constraints

Page 76: Unity3D - Computing Systemsbranko-cirovic.appspot.com/CP3830/Unity3D.Basics.pdf · Unity3D Unity3D is a powerful cross-platform 3D engine ... select Main Camera. ... (Maya, Sketchup

Movement

Not much movement so far. In order to move Player around the Ground, we have to take and process inputs (Keyboard, Accelerometer etc).

And for that we need to write code. First of all create another folder called Scripts.

Page 77: Unity3D - Computing Systemsbranko-cirovic.appspot.com/CP3830/Unity3D.Basics.pdf · Unity3D Unity3D is a powerful cross-platform 3D engine ... select Main Camera. ... (Maya, Sketchup

Movement

To create controller for our Player, select Player, Add Component, New Script. Name: PlayerController, Type: C-Sharp

You might have to move newly created script into Scripts folder

Page 78: Unity3D - Computing Systemsbranko-cirovic.appspot.com/CP3830/Unity3D.Basics.pdf · Unity3D Unity3D is a powerful cross-platform 3D engine ... select Main Camera. ... (Maya, Sketchup

C# Script

If you select PlayerController script, you can see in the Inspector that some code has already been generated for us.

However, code is not editable in Inspector. Double-click PlayerController and start MonoDevelop (editor)

Page 79: Unity3D - Computing Systemsbranko-cirovic.appspot.com/CP3830/Unity3D.Basics.pdf · Unity3D Unity3D is a powerful cross-platform 3D engine ... select Main Camera. ... (Maya, Sketchup

C# Scriptusing UnityEngine; using System.Collections;

public class PlayerController : MonoBehaviour {

// Use this for initialization void Start () { } // Update is called once per frame void Update () { } }

Page 80: Unity3D - Computing Systemsbranko-cirovic.appspot.com/CP3830/Unity3D.Basics.pdf · Unity3D Unity3D is a powerful cross-platform 3D engine ... select Main Camera. ... (Maya, Sketchup

C# Script

Start () method is called once in order to initialize variables. Which variables do we need?

Speed and reference to our player:

public float speed; private Rigidbody rb;

Page 81: Unity3D - Computing Systemsbranko-cirovic.appspot.com/CP3830/Unity3D.Basics.pdf · Unity3D Unity3D is a powerful cross-platform 3D engine ... select Main Camera. ... (Maya, Sketchup

C# Script

Start () method initializes our Player :

void Start () { rb = GetComponent<Rigidbody> (); }

Page 82: Unity3D - Computing Systemsbranko-cirovic.appspot.com/CP3830/Unity3D.Basics.pdf · Unity3D Unity3D is a powerful cross-platform 3D engine ... select Main Camera. ... (Maya, Sketchup

C# Script

Update () method is called once per frame and that’s where our animation goes.

For Desktop, Input are arrow keys and for phones, Input is Accelerometer by default.

What moves the Player is the force, which is the product of movement vector and the speed.

Page 83: Unity3D - Computing Systemsbranko-cirovic.appspot.com/CP3830/Unity3D.Basics.pdf · Unity3D Unity3D is a powerful cross-platform 3D engine ... select Main Camera. ... (Maya, Sketchup

C# Scriptvoid Update () { float moveHorizontal = Input.GetAxis ("Horizontal"); float moveVertical = Input.GetAxis ("Vertical"); Vector3 movement = new Vector3 (moveHorizontal, 0.0f, moveVertical); rb.AddForce (movement * speed); }

Try to run it now (hit play button in Unity). Nothing happens. Why?

Page 84: Unity3D - Computing Systemsbranko-cirovic.appspot.com/CP3830/Unity3D.Basics.pdf · Unity3D Unity3D is a powerful cross-platform 3D engine ... select Main Camera. ... (Maya, Sketchup

C# Script

What is the value of speed? Well, select Player and observe PlayerScript in Inspector:

Speed is 0 and so is the force. Set it to 10, run again and have fun!

Page 85: Unity3D - Computing Systemsbranko-cirovic.appspot.com/CP3830/Unity3D.Basics.pdf · Unity3D Unity3D is a powerful cross-platform 3D engine ... select Main Camera. ... (Maya, Sketchup

It’s alive!

Page 86: Unity3D - Computing Systemsbranko-cirovic.appspot.com/CP3830/Unity3D.Basics.pdf · Unity3D Unity3D is a powerful cross-platform 3D engine ... select Main Camera. ... (Maya, Sketchup

Camera

Our camera is static. How to have camera following our Player?

Select Main Camera and in Inspector lift it up (Y) and rotate by 45 degrees (X).

Page 87: Unity3D - Computing Systemsbranko-cirovic.appspot.com/CP3830/Unity3D.Basics.pdf · Unity3D Unity3D is a powerful cross-platform 3D engine ... select Main Camera. ... (Maya, Sketchup

CameraTo control camera, we need a script. Select Main Camera, Add Component, New Script. Name: CameraController, Type: C-Sharp

The offset of type Vector3 is constant - difference between Player and Main Camera.

To update camera’s position we simply add player’s position and offset in Update () method ( or LateUpdate () as recommended)

Page 88: Unity3D - Computing Systemsbranko-cirovic.appspot.com/CP3830/Unity3D.Basics.pdf · Unity3D Unity3D is a powerful cross-platform 3D engine ... select Main Camera. ... (Maya, Sketchup

CameraController.csusing UnityEngine; using System.Collections;

public class CameraController : MonoBehaviour { public GameObject player; private Vector3 offset; void Start () { offset = transform.position - player.transform.position; } void LateUpdate () { transform.position = player.transform.position + offset; } }

Page 89: Unity3D - Computing Systemsbranko-cirovic.appspot.com/CP3830/Unity3D.Basics.pdf · Unity3D Unity3D is a powerful cross-platform 3D engine ... select Main Camera. ... (Maya, Sketchup

Camera

The last thing we need to do here is to assign Player to our CameraController.

Select Main Camera and drag Player into the Player field in Inspector.

Run and observe that camera follows player even if player falls of the edge of ground.

Page 90: Unity3D - Computing Systemsbranko-cirovic.appspot.com/CP3830/Unity3D.Basics.pdf · Unity3D Unity3D is a powerful cross-platform 3D engine ... select Main Camera. ... (Maya, Sketchup

It’s alive!

Page 91: Unity3D - Computing Systemsbranko-cirovic.appspot.com/CP3830/Unity3D.Basics.pdf · Unity3D Unity3D is a powerful cross-platform 3D engine ... select Main Camera. ... (Maya, Sketchup

WallsIt’s quite easy (and frustrating) for that ball to drop of the plain.

We are going to add walls by creating an empty game object Walls, resetting it and adding four walls to it.

To create a new Wall, add new 3D game object Cube, rename it to West Wall, reset and drop into parent Walls

Page 92: Unity3D - Computing Systemsbranko-cirovic.appspot.com/CP3830/Unity3D.Basics.pdf · Unity3D Unity3D is a powerful cross-platform 3D engine ... select Main Camera. ... (Maya, Sketchup

WallsSelect West Wall and in inspector scale it to (0.5, 2, 20.5) and position to (-10, 0, 0)

Page 93: Unity3D - Computing Systemsbranko-cirovic.appspot.com/CP3830/Unity3D.Basics.pdf · Unity3D Unity3D is a powerful cross-platform 3D engine ... select Main Camera. ... (Maya, Sketchup

Walls

To create the East Wall, select West Wall, Edit, Duplicate and change its position to (10, 0, 0)

Similarly, create North Wall - position: (0, 0, 10), rotation (0, 90, 0) and South Wall - position (0, 0, -10), rotation (0, 90, 0)

Page 94: Unity3D - Computing Systemsbranko-cirovic.appspot.com/CP3830/Unity3D.Basics.pdf · Unity3D Unity3D is a powerful cross-platform 3D engine ... select Main Camera. ... (Maya, Sketchup

Walls

Page 95: Unity3D - Computing Systemsbranko-cirovic.appspot.com/CP3830/Unity3D.Basics.pdf · Unity3D Unity3D is a powerful cross-platform 3D engine ... select Main Camera. ... (Maya, Sketchup

Collectable ObjectsCreate new Cube, rename it to Pickup and reset Transform.

Select Cube and Edit, Lock View to Selected. Player is in the way.

Select Player and in Inspector, deselect checkbox in front of the Player

Page 96: Unity3D - Computing Systemsbranko-cirovic.appspot.com/CP3830/Unity3D.Basics.pdf · Unity3D Unity3D is a powerful cross-platform 3D engine ... select Main Camera. ... (Maya, Sketchup

Tranforms

Transform cube - position (0, 0.5, 0), rotation (45, 45, 45), scale (0.5, 0.5, 0.5)

We also want our Cube to permanently rotate - for that, we need a script.

Pickup selected, in Inspector add component, new script, C-Sharp, name: Rotator

Page 97: Unity3D - Computing Systemsbranko-cirovic.appspot.com/CP3830/Unity3D.Basics.pdf · Unity3D Unity3D is a powerful cross-platform 3D engine ... select Main Camera. ... (Maya, Sketchup

Tranformsusing UnityEngine; using System.Collections;

public class Rotator : MonoBehaviour {

// Use this for initialization void Start () { } // Update is called once per frame void Update () { transform.Rotate ( new Vector3 (15, 30, 45) * Time.deltaTime); } }

Page 98: Unity3D - Computing Systemsbranko-cirovic.appspot.com/CP3830/Unity3D.Basics.pdf · Unity3D Unity3D is a powerful cross-platform 3D engine ... select Main Camera. ... (Maya, Sketchup

PrefabsLet’s create more pickup objects. In order to do so we will create Prefab (template for game object)

Start by selecting Project and create new folder Prefabs. Drag the Pickup object from Hierarchy into Prefabs folder

Also in Hierarchy, create an empty objectname Pickups and drag Pickup into it

Page 99: Unity3D - Computing Systemsbranko-cirovic.appspot.com/CP3830/Unity3D.Basics.pdf · Unity3D Unity3D is a powerful cross-platform 3D engine ... select Main Camera. ... (Maya, Sketchup

PrefabsChange perspective to top (right click on gizmo)

Select Pickup from hierarchy and change its orientation from local to global

Page 100: Unity3D - Computing Systemsbranko-cirovic.appspot.com/CP3830/Unity3D.Basics.pdf · Unity3D Unity3D is a powerful cross-platform 3D engine ... select Main Camera. ... (Maya, Sketchup

PrefabsNow duplicate (⌘D) and arrange Pickups

Page 101: Unity3D - Computing Systemsbranko-cirovic.appspot.com/CP3830/Unity3D.Basics.pdf · Unity3D Unity3D is a powerful cross-platform 3D engine ... select Main Camera. ... (Maya, Sketchup

Materials again

Let’s change the color of our Pickups. In Assets, duplicate existing material (background) and in Albedo change its color.

Now simply drag newly created material into Prefabs/Pickup. Bingo!

Page 102: Unity3D - Computing Systemsbranko-cirovic.appspot.com/CP3830/Unity3D.Basics.pdf · Unity3D Unity3D is a powerful cross-platform 3D engine ... select Main Camera. ... (Maya, Sketchup

It’s alive!

Page 103: Unity3D - Computing Systemsbranko-cirovic.appspot.com/CP3830/Unity3D.Basics.pdf · Unity3D Unity3D is a powerful cross-platform 3D engine ... select Main Camera. ... (Maya, Sketchup

CollisionsIn order to pick up objects we have to detect when collision occurs between Player and Pickup

Open PlayerController.cs and add methodvoid OnTriggerEnter(Collider other)

void OnTriggerEnter(Collider other) { if (other.gameObject.CompareTag ("Pick Up")) { other.gameObject.SetActive (false); } }

Page 104: Unity3D - Computing Systemsbranko-cirovic.appspot.com/CP3830/Unity3D.Basics.pdf · Unity3D Unity3D is a powerful cross-platform 3D engine ... select Main Camera. ... (Maya, Sketchup

CollisionsFor this code to work we need to associate tag “Pick Up” with our Pickup prefab.

Select Pickup from Prefabs and in Inspector, Tag select Add Tag

In the dialog, add tag, name: Pick Up

Page 105: Unity3D - Computing Systemsbranko-cirovic.appspot.com/CP3830/Unity3D.Basics.pdf · Unity3D Unity3D is a powerful cross-platform 3D engine ... select Main Camera. ... (Maya, Sketchup

CollisionsBack in Inspector, prefab Pickup select, choose Pick Up tag. Observe that the same tag is associated with all pickup objects.

If we run the game now, there is not much difference - Player still bounces agains Pickups.

Select Pickup from Prefabs and in Inspector, Box Collider, check Is Trigger checkbox.

Page 106: Unity3D - Computing Systemsbranko-cirovic.appspot.com/CP3830/Unity3D.Basics.pdf · Unity3D Unity3D is a powerful cross-platform 3D engine ... select Main Camera. ... (Maya, Sketchup

Physics OptimizationCalculating collisions with rotating cubes (static cache) per each frame can be computationally expensive.

The solution is to add rigid body to our Pickup objects.

Select Pickup from Prefabs, Add Component, Physics, Ridid Body.

This will turn static coliders into dynamic coliders.

Page 107: Unity3D - Computing Systemsbranko-cirovic.appspot.com/CP3830/Unity3D.Basics.pdf · Unity3D Unity3D is a powerful cross-platform 3D engine ... select Main Camera. ... (Maya, Sketchup

Physics OptimizationRun.

Oops! All Pickups drop trough the floor (gravity pulls them down. They are triggers and do not collide with the floor)

One might be tempted to disable gravity in Rigidbody component

Page 108: Unity3D - Computing Systemsbranko-cirovic.appspot.com/CP3830/Unity3D.Basics.pdf · Unity3D Unity3D is a powerful cross-platform 3D engine ... select Main Camera. ... (Maya, Sketchup

Physics Optimization

This would work in our example, but not in general, since rigid body would still react to physics forces.

The solution is to declare Pickup as Kinematic - now it won’t react to physics forces.

Page 109: Unity3D - Computing Systemsbranko-cirovic.appspot.com/CP3830/Unity3D.Basics.pdf · Unity3D Unity3D is a powerful cross-platform 3D engine ... select Main Camera. ... (Maya, Sketchup

Display Text (Score)

In order to keep track of the score we need to enhance our PlayerController.

Select Scripts, PlayerController. To keep track of picked up cube we’ll declare:

private int count;

And set it to zero in our Start () method.

Page 110: Unity3D - Computing Systemsbranko-cirovic.appspot.com/CP3830/Unity3D.Basics.pdf · Unity3D Unity3D is a powerful cross-platform 3D engine ... select Main Camera. ... (Maya, Sketchup

Display Text (Score)

Count is incremented in our OnTriggerEnter method:

void OnTriggerEnter(Collider other) { if (other.gameObject.CompareTag ("Pick Up")) { other.gameObject.SetActive (false); count++; } }

Page 111: Unity3D - Computing Systemsbranko-cirovic.appspot.com/CP3830/Unity3D.Basics.pdf · Unity3D Unity3D is a powerful cross-platform 3D engine ... select Main Camera. ... (Maya, Sketchup

Display Text (Score)

To display count, we’ll need to use Unity’s UI Tool Set.

From Hierarchy, CreateUI Text

Page 112: Unity3D - Computing Systemsbranko-cirovic.appspot.com/CP3830/Unity3D.Basics.pdf · Unity3D Unity3D is a powerful cross-platform 3D engine ... select Main Camera. ... (Maya, Sketchup

Display Text (Score)

Observe that Unity also created Text’s parent object Canvas as EventSystem game object.

All UI elements must be children of the Canvas element.

Rename Text to Count Text and in Inspector change its color to white, reset position.

Page 113: Unity3D - Computing Systemsbranko-cirovic.appspot.com/CP3830/Unity3D.Basics.pdf · Unity3D Unity3D is a powerful cross-platform 3D engine ... select Main Camera. ... (Maya, Sketchup

Display Text (Score)We’ll position our text into the upper left corner of the screen using Rect transform Anchor Presets

Shift + Alt presents more options.

Padding and size can be changed in Pos X, Pos Y, Pos X, Width, Height fields of Rect transform

Page 114: Unity3D - Computing Systemsbranko-cirovic.appspot.com/CP3830/Unity3D.Basics.pdf · Unity3D Unity3D is a powerful cross-platform 3D engine ... select Main Camera. ... (Maya, Sketchup

Display Text (Score)

To wire up UI Text to display count value, open PlayerController script.

At the top of the script we need to declare namespace:

using UnityEngine.UI;

Page 115: Unity3D - Computing Systemsbranko-cirovic.appspot.com/CP3830/Unity3D.Basics.pdf · Unity3D Unity3D is a powerful cross-platform 3D engine ... select Main Camera. ... (Maya, Sketchup

Display Text (Score)

Next we declare reference to our UI Text component:

countText.text = "Count: " + count.ToString ();

public Text countText;

And initialize it in Start () method:

Page 116: Unity3D - Computing Systemsbranko-cirovic.appspot.com/CP3830/Unity3D.Basics.pdf · Unity3D Unity3D is a powerful cross-platform 3D engine ... select Main Camera. ... (Maya, Sketchup

Display Text (Score)Same code is executed in OnTriggerEvent () method after counter is incremented.

Finally, in Unity editor, select Player and observe that Count Text field has been created.

From Hierarchy, drag and drop Count Text into None (Text) field.

Page 117: Unity3D - Computing Systemsbranko-cirovic.appspot.com/CP3830/Unity3D.Basics.pdf · Unity3D Unity3D is a powerful cross-platform 3D engine ... select Main Camera. ... (Maya, Sketchup

Display Text (Score)Run.

Page 118: Unity3D - Computing Systemsbranko-cirovic.appspot.com/CP3830/Unity3D.Basics.pdf · Unity3D Unity3D is a powerful cross-platform 3D engine ... select Main Camera. ... (Maya, Sketchup

Game OverLet’s create another UI Text (name: Win Text, color: white, position: center top)

As before, in PlayerController.cs,declare Text variable

public Text winText;

Initialize it to an empty string:countText.text = "";

Page 119: Unity3D - Computing Systemsbranko-cirovic.appspot.com/CP3830/Unity3D.Basics.pdf · Unity3D Unity3D is a powerful cross-platform 3D engine ... select Main Camera. ... (Maya, Sketchup

Game OverAfter counter is incremented, see if it’s 12 - total number of pickup objects.

if(count >= 12) winText.text = "You win!";

Back in Unity editor, select Player and drag and drop Win Text from Hierarchy into Win Text placeholder.

Page 120: Unity3D - Computing Systemsbranko-cirovic.appspot.com/CP3830/Unity3D.Basics.pdf · Unity3D Unity3D is a powerful cross-platform 3D engine ... select Main Camera. ... (Maya, Sketchup

Build and RunFrom the main menu,

select File,Build Settings

Give it a name

Page 121: Unity3D - Computing Systemsbranko-cirovic.appspot.com/CP3830/Unity3D.Basics.pdf · Unity3D Unity3D is a powerful cross-platform 3D engine ... select Main Camera. ... (Maya, Sketchup

Build and Run