Learning Unity

Post on 23-Feb-2016

38 views 0 download

Tags:

description

Learning Unity. Getting Unity. http://unity3d.com/unity/download / http://www.microsoft.com/net/download. Parts of the Unity Workspace. Scene – Place objects Objects – View and select objects Pre-Built – Code and object template Variables – Modify existing objects. - PowerPoint PPT Presentation

transcript

Learning Unity

Getting Unity

• http://unity3d.com/unity/download/

• http://www.microsoft.com/net/download

Parts of the Unity Workspace

• Scene – Place objects

• Objects – View and select objects

• Pre-Built – Code and object template

• Variables – Modify existing objects

Object Oriented Programming

• Each object represents a thing or a collection of ideas

• Ex “Car”– Functions: (things to do) accelerator, turn, stop– Variables: (Features) color, speed, location– Skin: (Visual appearance) polygons in space• Relevant in Unity

Adding an Object

• Here we add a cube• Properties– Location– Rotation– Scale

• Change properties– Type in text– Use graphical interface

The Camera or Viewpoint

• Need to see scene from a given perspective

• Can have multiple perspectives– Viewport not what user sees– See camera preview

Running the program

• Click on the “play” arrow

• Click again to stop• See from the viewpoint

of the camera• Everything is dark,

need some lights– Add directional light

Adding Code

• To make it do anything, need to add code

• Select pre-built code – Rigidbody– This provides physics– Now object drops

• Note: drag the “game” tab to view scene and game view at the same time

Collision

• Add in a floor– New Cube object– Resize and move

• Now when you run– Cube drops to floor

Our Own Code

• Create a Javascript asset– Can program in multiple

languages in Unity• Name Script• Double click to edit

Our Own Code - 2• Code editor• Code starts with some basic things• Ignore “#pragma strict”• function Start() {}

– Runs when you hit the start button• function Update() {}

– Runs every time things change• Automatic tick (moves objects,

updates physics, updates graphics, calls Update functions etc)

Our Own Code - Follow

• Variable– var toFollow : Transform;– A variable is a space to hold

a value that might change• Update– this.transform.position =

toFollow.position;– Move this object to the

follow object

Adding Our Code

• We want the camera to follow the cube

• Simple click and drag code to the camera

• But what about the variable? We need to tell it what to follow

• So drag the cube to the ToFollow variable

Adding Our Code - 2

• Now when you run, the camera moves with the falling cube– Unfortunately you can’t see the

cube because you are inside of it!

– To fix, we could change the position code (to move it away)• But instead we will learn about

nesting

Nesting

• Right click on the script and select remove script

• Now drag the Main Camera onto the cube in the far left bottom window– This means that the camera

will move relative to the box– In this way, you can embed

multiple layers

Adding Assets• You can import assets which are

collections of code and objects created by other people

• Import the character controller and confirm the import

• The assets will appear in the bottom center window

• Note – I have deleted the main cube– In the hierarchy, drag the camera

off, then select the cube and hit the delete key

Prefab• Assets can contain images,

code, polygons, music, etc.• They can also contain Prefabs

– Prefabs are collections of objects and scripts to place in the world

• Select and drag the first person controller onto the screen

• In play mode, you can now move around (asdf, or arrow keys), space to jump, and look with the mouse

Back to Programming - Conditionals

• Create new script called FallOff• If you fall off of the world, this

will put you back at the beginning

• Conditional “if”, only run the code if true

• If the y coordinate is below a given level, then move to 0,0,0

• Then attach to your character– Experiment with running off

of the platform

A Simple Game

• Just by adding in a few platforms, we can make a game.

• Try to get from the left platform to the right one.