basics of scripting in unity - fullerton college · 1. after installing unity, start a new project....

16
Basics of Scripting in Unity Unity is a game engine that includes an editor, a set of tools, and a set of libraries and scripts. Unity uses the Object model to manage the various game elements. When you create a game in unity you will create various kinds of Unity Game Objects. These objects makeup the game and will automatically be run when the game runs. Game Objects in unity consist of a set of component and a set of game classes. Each game component is essentially a Script, which is executed by the Game Engine when the game runs. The Unity Game engine is responsible for running the game loop. The game loop looks at each game object and runs methods in the scripts corresponding to the default methods Unity is looking for. There are a number of components you can add to game objects. These are scripts which access the various Unit classes and objects and that provide functionality. You can also create your own scripts and attach these to the game objects.

Upload: others

Post on 31-Jul-2020

5 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Basics of Scripting in Unity - Fullerton College · 1. After installing Unity, start a new project. Make sure you click the 2D button and use the name testscripts. 2. Click the Create

Basics of Scripting in Unity

Unity is a game engine that includes an editor, a set of tools, and a set of libraries and scripts. Unity uses the Object

model to manage the various game elements. When you create a game in unity you will create various kinds of Unity

Game Objects. These objects makeup the game and will automatically be run when the game runs.

Game Objects in unity consist of a set of component and a set of game classes. Each game component is essentially a

Script, which is executed by the Game Engine when the game runs. The Unity Game engine is responsible for running

the game loop. The game loop looks at each game object and runs methods in the scripts corresponding to the default

methods Unity is looking for.

There are a number of components you can add to game objects. These are scripts which access the various Unit classes

and objects and that provide functionality. You can also create your own scripts and attach these to the game objects.

Page 2: Basics of Scripting in Unity - Fullerton College · 1. After installing Unity, start a new project. Make sure you click the 2D button and use the name testscripts. 2. Click the Create

When you install the new Unity version it includes an external editor for scripts. This is either MonoDevelop or Visual

Studio. There is an option in Unity to choose the external editor you wish to use to edit Scripts. You don’t have to worry

about compiling because Unity has a built-in script compiler.

1. After installing Unity, start a new project. Make sure you click the 2D button and use the name testscripts.

2. Click the Create Project button. You see the default screen layout.

Windows can be moved and resized. The Hierarchy screen displays the elements of the current Scene. The Scene

window displays the graphical representation of the Scene. The Game tab (next to Scene) will display the output screen.

Page 3: Basics of Scripting in Unity - Fullerton College · 1. After installing Unity, start a new project. Make sure you click the 2D button and use the name testscripts. 2. Click the Create

The Project window displays the folders used to save game assets. The Console tab (next to Project) displays output

messages and debugging messages from scripts. The Inspector window displays information about game objects.

3. Click and drag the Game tab so it is below the Scene tab.

4. Drag the project tab over until it is below the Inspector tab.

Page 4: Basics of Scripting in Unity - Fullerton College · 1. After installing Unity, start a new project. Make sure you click the 2D button and use the name testscripts. 2. Click the Create

5. Drag the Console tab until it is below the Project tab:

6. Drag the Hierarchy tab over until it is next to the Inspector tab:

Page 5: Basics of Scripting in Unity - Fullerton College · 1. After installing Unity, start a new project. Make sure you click the 2D button and use the name testscripts. 2. Click the Create

7. You can save your layout by clicking the Layout option button, choosing Save Layout, and giving it a name:

The default name for your Scene is Untitled. This can be changes when you save the scene. Before saving the scene you

want to create a Scenes folder and store the scene in this folder.

8. Right click the Assets folder, choose Create, and choose Folder.

Page 6: Basics of Scripting in Unity - Fullerton College · 1. After installing Unity, start a new project. Make sure you click the 2D button and use the name testscripts. 2. Click the Create

9. Type the folder name Scenes. It appears under the Assets folder.

10. Using these steps, create folders for Sprites and Scripts.

11. Click the File menu, choose Save Scenes, and save your Untitled scene in the Scenes folder with the name

Scene1.

Page 7: Basics of Scripting in Unity - Fullerton College · 1. After installing Unity, start a new project. Make sure you click the 2D button and use the name testscripts. 2. Click the Create

12. In the Hierarchy window click the triangle next to the Scene1 object to expand this object. Under this Scene is an

object named Main Camera. Click this Main Camera object and look at the Inspector window.

Main Camera is a built-in object. The Inspector window displays all the default scripts and other objects that are linked

with Main Camera.

Creating a Game Object

1. Find and download a PNG image. Open this image in a graphics editor and resize it to 128 pixels in width. (I have

a link to a Tux the Penguin image on my web site.)

2. Click the Sprites folder name in the Assets folder.

Page 8: Basics of Scripting in Unity - Fullerton College · 1. After installing Unity, start a new project. Make sure you click the 2D button and use the name testscripts. 2. Click the Create

3. Drag the downloaded image into this folder. It will show up in the Project window.

4. Drag the Sprite into the Scene. It appears in the Scene window and also appears in the Hierarchy list:

Page 9: Basics of Scripting in Unity - Fullerton College · 1. After installing Unity, start a new project. Make sure you click the 2D button and use the name testscripts. 2. Click the Create

5. Look at the information displayed in the Inspector window when this object is selected:

Notice that a Transform object has automatically been attached. We next want to create a simple Script to move the

object left and right.

6. Right click the Scripts folder, choose Create, and choose C# Script. A new Script will appear in the Scripts folder:

Page 10: Basics of Scripting in Unity - Fullerton College · 1. After installing Unity, start a new project. Make sure you click the 2D button and use the name testscripts. 2. Click the Create

7. Double click this script and it will be opened in your editor. If this does not open you can select the editor you

wish to use with scripts by clicking the Edit menu, choosing Preferences, and choose External Tools. You can use

MonoDevelop or Visual Studio (or some other editor).

8. In Visual Studio your code looks like:

The code represents a Class which will be used by Unity to create the object component that will be attached to the

game object. Notice that the class is named after the name of the script “MoveObject” and extends from MonoBehavior.

There are two default methods in this script, Start() and Update(). These will be automatically called by the Unity game

engine when the game runs. The Start() method is called after the game resources have been loaded and before the

start of the game loop and it is only called once. The Start() method is used for setup and initialization. The Update()

method is called each time through the game loop

Page 11: Basics of Scripting in Unity - Fullerton College · 1. After installing Unity, start a new project. Make sure you click the 2D button and use the name testscripts. 2. Click the Create

9. Edit the code so it look like this image:

Looking at this code, there are a few important elements.

Directly under the class declaration are the public and private variables speed and Vector3.

Any public variable declared at the class level will automatically appear in the Inspector window when this script is

added to an object. The speed variable is initially set to 2.0 but can be changed through the Inspector. The private

variable Vector3 is a Unity class which holds three numbers representing change in position (x,y,z). The x and y values

denote movement left and right (x) and up and down (y). The z value represents forward and backward and only works

for 3D environment. However, since the Vector3 class is widely used it is easier to just set z to zero rather than keep

converting it to a Vector2.

In this case the speed variable holds how fast the object moves and the Vector3 holds how far it will move.

Page 12: Basics of Scripting in Unity - Fullerton College · 1. After installing Unity, start a new project. Make sure you click the 2D button and use the name testscripts. 2. Click the Create

The Update() method contains all the rest of the code:

The command:

pos = new Vector3(Input.GetAxis(“Horizontal”), Input.GetAxis(“Vertical”), 0)

creates a new Vector3 value and assigns it to the pos variable. But that values are being set?

The commands Input.GetAxis(“Horizontal”) and Input.GetAxis(“Vertical”) will return either a negative or positive value

depending on whether the object is moving left/right or up/down. Input.GetAxis(“Horizontal”) returns a negative value if

the object is moving left and a positive value if the object is moving right and Input.GetAxis(“Vertical”) returns a positive

if moving up and negative if moving down.

There is an Input object in Unity which manages keyboard and controller input. This is called Input. You can access the

Input object from the Edit/Project Settings/Input option. Looking at the Inspector window for the Input object you see

(after expanding Axes) a list of input events.

Page 13: Basics of Scripting in Unity - Fullerton College · 1. After installing Unity, start a new project. Make sure you click the 2D button and use the name testscripts. 2. Click the Create

Expanding the first Horizontal option you see:

Notice that the Negative Button and Positive Button are left/right and the alternate is a/d. Also notice that the name of

the Horizontal element is “Horizontal”. This identifies the horizontal Input object and lets you set various keys/controls

to move horizontally. The Vertical settings are the same except for the name “Vertical” and up/down and w/s

Now that you have the script created you can attach it to the game object.

Page 14: Basics of Scripting in Unity - Fullerton College · 1. After installing Unity, start a new project. Make sure you click the 2D button and use the name testscripts. 2. Click the Create

10. Drag the script and drop it on the game object in the Hierarchy list:

This automatically attaches the script to the object.

Page 15: Basics of Scripting in Unity - Fullerton College · 1. After installing Unity, start a new project. Make sure you click the 2D button and use the name testscripts. 2. Click the Create

11. Click the game object and look at the Inspector window.

The MoveObject script has been attached to the game object and the public variable Speed is now exposed through the

Inspector. You can change this value from the Inspector and it will affect the script. What actually happened is that the

Script was compiled and an object created and then attached as a component to the game object.

12. Click the Run button to run your game:

You can now move your object left/right and up/down using the arrow keys, the wasd keys, or a controller. To make

your object go faster you can change the speed from the Inspector window for this object.

13. Click the Run button again to stop your program.

Page 16: Basics of Scripting in Unity - Fullerton College · 1. After installing Unity, start a new project. Make sure you click the 2D button and use the name testscripts. 2. Click the Create