Making a Game with Unity
Download and install Unity (Personal)
Start it
Sign-in if necessary
Create a new project
Set the project to be in 3D
Turn off Unity analytics
Finding your way in Unity
Games are made up of different levels or screens
Unity calls them "Scenes"
You can edit one scene at a time
Unity has different panels showing different views and tools for your game
Project shows all the files used to make up your game
Scene is a graphical view of the objects in the scene you are working on
Hierarchy is a text view of the scene
Game is where you can play a scene of your game to test it
Inspector is where you can view and edit the properties of game objects
In a new project, Unity creates an empty scene for you
Save the scene (to the default Assets folder) and give it a name
After saving your scene, you'll see the saved file in the Project panel
Make some ground
In the menus, choose GameObject…3D Object…Plane
Resize the plane to be bigger by adjusting the X and Z scale in Inspector
Instead of typing in numbers, you can put your mouse over the "X" or "Z" and drag them
Nothing changes when you change the Y scale--why?
Extra: Navigating around a scene
You can rotate the scene by holding down the Alt-key then dragging the mouse
You can move the scene by holding down the Alt-key and Ctrl-key then dragging the mouse
Create a material in the Project
In the menus, choose Assets…Create…Material
Rename the material to "ground material"
In the Project panel, click it once to select it, pause, click it a second time, then rename it
Set the Albedo to green (click beside the eyedropper)
Drag the Material to the Plane of the Scene or Hierarchy
Add a character
Download block characters from
[Link]
Unzip it
Drag the "Blockly Characters/Unity/[Link]" file to Project to import it
In the Project, drag "Character Model/Model/basicCharacter" (or advancedCharacter) to Scene (or
Hierarchy)
Expand the character in the Hierarchy to see the different parts that can have materials and the rig
In the Project, drag "Character Mode/Material/…" to the parts of the characters in the Scene or Hierarchy
Drag the materials (the round circle) and not just the texture (ie the picture))
Make the character bigger by setting the scale to X: 5, Y:5, Z:5
Posing the character
In the Hierarchy view, go into the characters rig
Select a leg, then change its rotation in the Insector to move it
Enter into the rig of the spine and rotate the neck to to move the head
To reset, select the item, click the gear on the Transform, choose "Revert to Prefab"
Add some surroundings
Download some nature art from
[Link]
Unzip it
Drag "naturepack_extended/Unity package/naturepack_unity.unitypackage" to the Project panel
Import it all
Drag some nature stuff to your scene
Add some collision detection to the character
Choose character
In the Inspector, choose "Add Component…Physics…Character Controller"
You'll notice a big round "pill" shape around your character
This is the collision area for the character
It's much too big
Set Center Y to 0.1
Set Height to 0.2
Set Radius to 0.05
Set Skin Width to 0.01
Extra: Collision Detection
In a game, you don't want your character to be able to go through walls
Games use collision detection to detect when two objects overlap
If they do "collide", the objects can be moved back
Characters have arms and legs that move around, making collision detection hard
For characters, the game will use the simpler "pill" shape for the collision detection
You want the collision area to be close to the same size as the character
Programming some movement
Choose the character
In the Inspector, choose "Add Component…New Script" and make a new C# script called
PlayerController
Edit the script (choose the gear icon and edit, or select it in the Project and choose Open in the Inspector)
In Update(), add the code:
public class PlayerController : MonoBehaviour {
…
void Update ( ) {
CharacterController cc = GetComponent<CharacterController> ();
Vector3 move = new Vector3 ([Link] ("Horizontal"), 0, [Link] ("Vertical"));
[Link] (move);
}
}
If you are using MonoDevelop for editing files:
After editing each file, you have to save it, then choose "Build…Build All" from the menus before
returning to Unity
Sometimes, MonoDevelop reports errors at startup
You may have to choose the Assembly-CSharp from the Solution pane, and reload it
Extra: Switching to MonoDevelop
In Windows, Unity uses Visual Studio for editing code
Visual Studio requires you to create a Microsoft account
Unity lets you use a different editor program called MonoDevelop if you don't want to use Visual
Studio Code
To switch, choose Edit…Preferences from the menu
Go to the "External Tools" tab
Switch the External Script Editor from Visual Studio to MonoDevelop
Run your game
Switch back to the main Unity program and run your game to see if you can move your character by
pressing the arrow keys
To run your game, press the Run triangle at the top of the window
You sometimes have to click on the Game panel itself to make sure your keystrokes are sent to the
game
To stop the game, click on the Run triangle button again
You can modify the game while it is running, but your changes will be lost when the game stops, so
be sure to stop the game before making any changes
Stop walking through walls
You will notice that your character can walk through walls and other objects
You may want to choose "Add Component…Physics…Mesh Collider" in the Inspector to them so that you
can’t go through them
Collision detection prevents objects from overlapping
The collision areas of the game objects haven't been set yet
A mesh collider treats the whole object as a collision area (not like the pill-shaped area used for the
character)
Be sure that you add the Mesh Collider to an object with a mesh
The mesh collider checks for collisions with a mesh, so the object needs have a mesh
If the Inspector shows a "Mesh Renderer" component, then it's the correct object to add the collider
to
You sometimes have to look inside game objects in the Hierarchy to find the mesh
Extra: What is a mesh?
3D math is hard
Most programmers and graphics hardware can only do the math for simple triangles in 3D
More complicated shapes are too difficult
As a result, 3D models of objects are represented as lots of triangles in games
These triangles form a mesh
To show a 3d object on the screen, a game engine draws all the triangles of the triangle mesh to the
screen
Programming the camera to follow the player
Choose the Main Camera in the Hierarchy
Add a new script called FollowCamera
The camera needs to know which object it should follow
Add these lines to the beginning:
public class FollowCamera : MonoBehaviour {
public GameObject follow;
public Vector3 offset;
…
}
Drag the character from the Scene Hierarchy to the "follow" variable of the script in the Inspector
Then add this code
public class FollowCamera : MonoBehaviour {
public GameObject follow;
public Vector3 offset;
…
void LateUpdate ( ) {
[Link] = [Link] + offset;
}
}
In the Inspector, set the Transform rotation to X: 45
In the Inspector, set the script's offset to X: 0, Y: 2, Z:-2
Adjust the player movement so that the player turns
Add this to the movement code to Update():
public class PlayerController : MonoBehaviour {
…
void Update ( ) {
CharacterController cc = GetComponent<CharacterController> ();
Vector3 move = new Vector3 ([Link] ("Horizontal"), 0, [Link] ("Vertical"));
[Link] (move);
if (move != [Link]) {
[Link] = [Link](move);
}
}
}
Create a walking animation
In the menus, choose Window…Animation to open the animation window
Dock the window somewhere
Choose your character in the Hierarchy view
In the Animation Window, click Create (or click on the animation clip name and choose Create New Clip)
Name it PlayerWalk
Click on time 0:15
You have to click on the timeline bar at the top
Or you can just type in the number in the field to the left of the timeline bar
Hit Record (the red circle)
Adjust Scene view so that you can see the character
In the Hierarchy view, go into the character’s rig, and select LegL1
Adjust the Y rotation so that it’s forward
Select the rig’s LegR1, adjust the Y rotation so that it’s back
Hit the Record button again to stop recording
Hit Play (the triangle) in the Animation window to see what you’ve got
Hit Play again to stop the animation
Click on time 0:45
Hit Record, move the LegL1 to be back and LegR1 to be forward
Click on time 0:60
Move the legs back to the standing position
Hit Record to turn off record mode
Hit play to see what you’ve done
It’s a little jerky
Click on Curves
Select LegL1:Rotation…Rotation.y so that its curve appears
Adjust the zooming scrollbar so that you can see the full curve
Notice that when the animation repeats, the curve has a little "bobble" in it
Click on the end point, and an extra curve "handle" appears
Move the handle to change the angle of the curve at the end
Also adjust the curve handle for the start point so that the curve is smooth at the end
Do the same with LegR1
Extra: Animation Key Frames
Making animations takes a long time
To save time, Unity lets you use key frames
You tell Unity how an object should look at certain "key" times (i.e. at key frames)
The game engine will then guess how the object should look at the times in-between the key frames
This process is known as in-betweening or tweening
Hooking in the Animation
Click on the character in the Hierarchy or Scene
In the Inspector, go to Animator…Controller
Double-click it to open the Animator Controller
There should already be a state called PlayerWalk
Right-click (or Ctrl-Click) and choose "Create State…Empty"
In the Inspector, name the new state "Idle"
Change the Update() code in PlayerController script to:
public class PlayerController : MonoBehaviour {
…
void Update ( ) {
CharacterController cc = GetComponent<CharacterController> ();
Vector3 move = new Vector3 ([Link] ("Horizontal"), 0, [Link] ("Vertical"));
[Link] (move);
Animator anim = GetComponent<Animator> ( );
if (move != [Link]) {
[Link] = [Link](move);
[Link] ("PlayerWalk");
} else {
[Link] ("Idle");
}
}
}
Jumping
Change the movement code in the PlayerController script:
public class PlayerController : MonoBehaviour {
private float ySpeed = 0;
…
void Update ( ) {
CharacterController cc = GetComponent<CharacterController> ();
if ([Link]) {
ySpeed = 0;
} else {
ySpeed -= 9.8f * [Link];
}
if ([Link] ("Jump") && [Link]) {
ySpeed = 5;
}
Vector3 move = new Vector3 ([Link] ("Horizontal"), 0, [Link] ("Vertical"));
[Link] (move);
[Link] (new Vector3 (move.x, ySpeed, move.z) * [Link]);
Animator anim = GetComponent<Animator> ( );
if (move != [Link]) {
[Link] = [Link](move);
[Link] ("PlayerWalk");
} else {
[Link] ("Idle");
}
}
}
Make some jumping platforms
Use cube objects to make some platforms to jump to
Adjust the speed of the character to a comfortable speed
Make some things you can collect or interact with
Make a sphere game object
Move it somewhere
In the Inspector, go to the collider and set “Is Trigger”
Optional:
Add a Physics…Rigidbody component to it (allows the object to move around—you may want that
later on)
In the Rigidbody component, turn off "Use Gravity" but turn on "Is Kinematic"
On the character, add this code:
public class PlayerController : MonoBehaviour {
…
public AudioClip clip;
void OnTriggerEnter(Collider other) {
if ([Link] == "Sphere") {
[Link] (false);
GetComponent<AudioSource> ( ).PlayOneShot (clip);
}
}
}
Then, add an AudioSource to the character and set an audio clip as appropriate)
Adding background music
Add an AudioSource to the camera
Set the AudioSource to "Play on Awake" and have it "Loop"
Choose a file for the clip to play it
Extra
Also useful:
[Link]("…");
[Link]("…");
[Link]("…");