Unity Game Engine (Basics)

advertisement
Unity Game Engine (Basics)
Written by Austin Rothermund
2015
+How to create a
“Jumper” Platformer in
Unity 3D
What is Unity?
Unity is an advanced 3D game development program with real time editing, built in
terrain creation, and interactable scripts. (two languages work together, C# and
Javascript)
Unity is not
Unity is
-3D modeling software.
-2D/3D game capable game engine.
-Animation studio.
-Works on nearly all platforms.
-Picture editor.
-Very user friendly, moderate to learn.
How to install
Most computers have Unity already installed, however if it is not on your computer
follow these steps.
1.
2.
3.
4.
5.
Go to Unity3D , and download personal edition free download at the bottom of the page. (It will direct you to the Unity download
installer)
Once downloaded launch the executable file and follow the Download Assistant.
When asked to choose components leave the default selected.
Install to C:\ drive.
After installation Unity may ask if you want pre built assets and examples, install if you wish.
Many of the prebuilt assets are very cool/fun to work with and test.
Once installed
Now that you have Unity installed you can start by creating a new project, launch
the Unity.exe on the desktop. It will then prompt you to create an account.
This account is important so try to remember your credentials. Once created and logged in you will not have to login again.
This will be used to access the asset store which will be discussed later in this tutorial.
A
fter creating an account create a new project, 3D
or 2D. I suggest beginners to start with 3D
because it holds the exact same components as
2D. Create a project name then press ok/create.
Then the engine will load.
What am I looking at?
You will now be seeing a large gray area with some columns on the edges. This is the area where you will be working on
all projects. First thing I suggest you do is to set the layout of the window to the 2 by 3 layout. Change this by going to the
top of the screen and pressing Window ---> Layouts ----> 2 by 3. Once this is done move the Project tab to the bottom of
your Hierarchy tab.
Once done
it should
look like
this -->
What am I looking at continued
Let's take a look at the Hierarchy. Inside this we will find two objects. A Main
Camera and a Directional Light. Think of the Main Camera as the eyes of the
player. If we delete the Main Camera you will not be able to see anything in the
game window. The Directional Light object is the lighting of the current level.
Unity has many types of light, however Directional Light works kind of like a sun
where it shines in all directions for an infinite range and duration. Now that we
know what those both are let's create my favorite object the Cube.
To create a cube object Right Click in the Hierarchy in a blank area and select 3D
object ---> Cube. This cube comes with many components such as a Box
Collider, (prevents collision) Cube (mesh filter), (color/skin of object) Mesh
Renderer, (how it interacts with light) And lastly Transform (objects orientation and
scale)
Cube Design
Now let’s make the cube a different color and larger. To do this first go to the
Assets folder at the bottom of the project tab and Right Click to create a new
folder. Name it Materials. Inside this folder will be your 3D materials/textures that
you will create. Now go inside your empty Materials folder and right click, select
Create ----> Material. This will create a white sphere, press f2 on the material and
rename it to Material 1, or something along those lines. Think of this as color at
the moment. Left Click your newly created material and look inside the inspector
window. Now we will change it’s color. To do this look under Main Maps and left
click the white box. Now select the color of your choice, I will use a nice teal color.
If you did this correctly you will have your color on the material. Now left click and
drag the colored material onto the cube.
Cube Design Continued
You will now notice that the cube has become the same color of your material. Now let’s increase the
scale of the cube and make it a bit larger. To increase the size of an object we will Left Click on the Cube
object in the Hierarchy and look at the inspector window. Inside the Inspector window is a component
called Transform, we will be specifically looking at the scale values. Change the values of the X, Y, and Z
to 5 each. Once that's done your Cube will be 5 times larger in all directions.
Controlling the Screen
Now that we have something to look at, we can change the way we look at the
scene. To do this hold down your right mouse button in the Scene window and
move with the WASD keys. Alternately you can scroll out using the middle mouse
button.
Now let’s duplicate the cube and make three more. To do this Left Click the Cube
in the Hierarchy and press Ctrl+D, to Duplicate or Ctrl+C To Copy (followed by
Ctrl+V to paste). These keyboard shortcuts do the same thing, I normally use
copy and paste. Now once you create three more identical cubes you will notice
that they are actually in the Exact same X,Y, and Z coordinates, because of this
you must move them by using the directional tool at the top left and move the
cubes X, and Z Axis. On the next slide you will see what my layout looks like.
Adding a Character
Now that we have some platforms with small
gaps between them, we can now add a first
person character to the scene. To get a first
person character you must Import a Unity
package. To import Right Click the Unity
Assets folder and go to Import Package --->
Characters. Once selected Unity will load in
the built in Character Package. A bunch of
checkboxes will now show up. Leave all of
them checked, however we will only be using
the First Person Character Package in this
tutorial, select Import. Once imported you will
get two new folders inside your Assets Folder.
Standard Assets is the folder we need to
navigate to our First Person Character.
DELETE THE MAIN CAMERA
IN HIERARCHY NOW!
Adding the First Person Character
Now navigate through your Standard Assets folder ----> Characters ---->
FirstPersonCharacter ----> Prefabs. Prefabs are objects in Unity that act like
global variables or already built objects. If you change a Prefab within the Asset
Folder each of that specific Prefab will also be changed within the Scene. Now let’
s drag the FPSController into our scene. Place it ontop of one of your cubes.
(You will notice prefabs in your Hierarchy have their text shaded blue.) We will
now give the FPSController a “Body” to do this Right Click the FPSController in
your Hierarchy and go to 3D Object ----> Capsule. Now move the capsule from
your scene slightly up the Y axis so the base of the capsule rests on the cube.
Great Job!
You have now created your first FirstPersonController in Unity. This
FPSController can Run(shift) Jump(Space) Move(WASD) and Turn
(MouseMovement). Also it includes built in gravity code, hitbox, sound effects and
much more useful components which can be looked up Character Controllers.
To play with this new Character press the “PLAY” Button at the top of the scene,
however before pressing the “PLAY” Button press Maximize on Play to fullscreen
the game when testing. Now that you are in test mode you can play around with
your character. Developers use this mode to test out their new creations and
manage their game. Note that you cannot edit while in tester mode!
Saving Your Work
Now that we got the basics of creating a character and making cubes to jump
on/walk on, let’s save our work. To do this go to the top and press File--->Save
Scene, name it anything you want, I named mine Test, then press Save.
Scripting In Unity
Scripting is another term for coding. In Unity we use two different programming languages, C# and Javascript. Unity allows
both types of code to interact with each other. You can learn more about C# and Javascript at the Unity - Scripting API.
We will now be creating a script that when the player falls off the platform he will be teleported back to the starting platform.
You can think of it like a checkpoint. To create your very first script create a new folder inside your Assets Folder, and
name it Scripts. This folder will hold all future Javascript and C# Scripts. Once the folder is created Right Click inside the
folder and go to Create---->C# Script. Name the script Teleporter, once created double click the script and it will open up
MonoDevelop(a built in Unity scripting IDE) or Visual Studio, this all depends on what is installed on your system, most
computers will have Visual Studio open up. In this tutorial I will be using Visual Studio, do not worry, coding is the exact
same in both IDE’s. Now let’s show you the code you will be using, I suggest you type the code so you learn, however
copying and pasting is sufficient as well.
Teleporter.cs
Here is the code, with line by line explanations:
1.
using UnityEngine;
2.
This line is already added automatically when creating a
new script
Also added automatically, do not worry about this.
using System.Collections;
3.
public class Teleporter : MonoBehaviour {
public GameObject TeleportTo;
void OnTriggerEnter(Collider other){
Vector3 displacement = other.transform.position;
other.transform.position = TeleportTo.transform.position;
}
}
We are creating a class called Teleporter, this is the
Exact case sensitive name as we named the file, if you
named the file ANYTHING else change this field.
4. Public (anything can access it) GameObject (The object that
will have the script on it) TeleportTo (Variable Name) will show
later)
5. When entering a collider (remember box colliders on the
cubes? We will make a special one for Triggers!)
6. Vector 3 is position of an object, displacement is what will be
happening to the object (pretty much motion and position) Will
then equal the motion/position of the “Entering” object (player).
7.Restating that the Position is going to be equal to the Variable
at the top GameObject's position.
So in general what did it do?
The script pretty much says once the Player object enters a Collider hitbox change the position of the Player object to the
other desired Object.
Now let’s set it up! (remember to Ctrl + S to save)
First create a new Cube Object large enough to go under all of your floating cubes. Name this cube Ground. When the
player falls on this ground you will be teleported to the starting Cube.
Setting up falling “Hitbox”
This might get a little complicated but just follow it slowly.
1.
2.
3.
4.
5.
6.
On the Ground Object look at the Inspector Window and select Add
Component.
In the search box Type in Teleporter, and left click the script.
Now that you have the Teleporter script you will now notice that there is an
empty field which states None(Game Object).
From your Hierarchy Select the Cube you want to be the starting position and
drag it into the empty Teleporter field.
On the Ground Object in the Box Collider field, make sure the checkbox is
marked for “ Is Trigger “
Once finished Jump off the ledge with the player and you should reset to the
chosen Object.
*Incase of an error*
To make the checkpoint more accurate create a small cube directly above the
Checkpoint cube. Once created turn its mesh renderer off, set it to Is Trigger, and
then drag it from the Hierarchy into the Teleporter script field.
Also delete the capsule inside your FPSController since you no longer need it
since it is a first person game. (optional)
Once done press (Ctrl + S) and save your work.
Now that you know how...
Now you have a pretty basic and fun game nearly complete.
All you must do now is create some more 3D Objects build a
larger level. Feel free to mess around with the other 3D
Objects, Spheres, Cylinder, 3D Text; By right clicking the
Hierarchy and creating 3D Object. Have around 20 new
objects of varying sizes and make more colored materials to
place on them.
EXAMPLE of how the
game CAN look.
Closing Words
But ultimately realize that Unity is what you make of it, make
a nice level layout or create amazing Scripts. It’s all up to
what YOU, the game dev want to make. Nothing is
impossible, have fun and create your own masterpiece.
This concludes Unity basics and how to create a jumper game.
Advanced tutorials will be on the attached youtube videos showing
how to use lighting, Terrain, Title Screens, and creating Exe files.
Watch some of this guys videos
Ryandome Youtube
Teaches nearly everything about unity and how to make a game from the ground
up.
Download