19
Learning Unity

Learning Unity

  • Upload
    fraley

  • View
    38

  • Download
    0

Embed Size (px)

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

Citation preview

Page 1: Learning Unity

Learning Unity

Page 2: Learning Unity

Getting Unity

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

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

Page 3: Learning Unity

Parts of the Unity Workspace

• Scene – Place objects

• Objects – View and select objects

• Pre-Built – Code and object template

• Variables – Modify existing objects

Page 4: Learning Unity

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

Page 5: Learning Unity

Adding an Object

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

• Change properties– Type in text– Use graphical interface

Page 6: Learning Unity

The Camera or Viewpoint

• Need to see scene from a given perspective

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

Page 7: Learning Unity

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

Page 8: Learning Unity

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

Page 9: Learning Unity

Collision

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

• Now when you run– Cube drops to floor

Page 10: Learning Unity

Our Own Code

• Create a Javascript asset– Can program in multiple

languages in Unity• Name Script• Double click to edit

Page 11: Learning Unity

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)

Page 12: Learning Unity

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

Page 13: Learning Unity

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

Page 14: Learning Unity

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

Page 15: Learning Unity

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

Page 16: Learning Unity

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

Page 17: Learning Unity

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

Page 18: Learning Unity

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

Page 19: Learning Unity

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.