EmojiInvaders: Unity3D Quick Introduction
ACM Student Chapter
Dr. David Cline
Saturday, October 22, 1:00 - 4:00 PM
- Setting up the Macs in the mac lab
- Getting started with Unity
- Unity interface basics
- The Scene window
- Placing objects and setting basic materials
- Running the game
- Making things move with Scripts
- Making bounds for motion based on scene objects
- Making cutouts (sprite-like objects)
- Turning Scene objects into Prefabs
- Spawning and destroying objects
- Special effects
- Building the game
Setting up the mouse on the macs
- Click the apple (top left corner of screen)
- Choose System Preferences and Mouse
- Change buttons to
- Primary, Secondary, Button3
- Choose scroll direction (turn off natural to be like Windows)
- Choose tracking speed according to your preference.
Unity3D is a commercial game engine that is free for educational and personal use. It is already installed on the MAC computers. If you are installing on your own laptop, it may take a few minutes. You can do it at Unity3D.com.
- Running Unity
- On the finder app (magnifying glass) type unity
- Click on the Unity app
- Click “Create an account”
- Create a Unity ID if you don’t already have one
- Log in to your email and confirm the email from Unity by clicking on the link.
- Log in to Unity3D, choose Personal edition
- Choose “I don’t use Unity in a professional capacity”
- Fill in the little survey
- Then you can start using Unity for the first time. Whew!
- Create a new project
- Make a folder on your desktop for unity projects
- Call your project emojiInvaders
- Make the project 3D
Parts of Unity interface
- Scene window
- Gives a visual representation of things in the current scene
- A scene is essentially like a level in a game
- Game window
- Shows how the game will look when running
- Shows a schematic view of the current scene
- Shows properties for currently selected object
- Allows you to see and change properties
- Shows all of the assets in the current project
In the Scene window
- Default objects
- (Camera and Directional Light)
- Looking around
- Right click and hold: fps-like controls
- Middle click and drag: pan
- Scroll wheel: zoom in and out
- Double click on object in hierarchy and view focuses on it
- Selecting object
- Left click selects
- Shift-Left click selects multiple
- Left click and drag
- Shift-left click and drag
- Global vs. Local axes
- Adding default objects
- GameObjects->3D objects
- We can set the location of an object in the inspector
An exercise in placing objects
- Add a sphere
- Set it’s location to (0, 0.5, 0)
- Add a plane
- Set it’s location to (0, 0, 0)
- Scale it by (10, 10, 10)
- Orient your view so you are looking approximately through the camera
- Now let’s try to set materials for the two objects
- Assets->Create material
- Name it the sphere material
- In Project, drag material to sphere
- Open Inspector and click on the sphere
- Quite a few properties
Running the game
- Press play button
- This causes the game to play, switching to the game view
- Pressing play again stops the game
Making the ball move
- To make the ball move, we use a script written in C#
- Click Assets->Create->C# Script
- In Project, rename the script to ballMover
- Attach script to ball
- Double-click on script. It should come up in an editor.
- Note how the script looks a lot like a Java class
- There are two functions:
- Start: called once when the ball is created
- Update: called every frame while the ball is active
- In update, we will move based on whether the arrow keys are down
private static float speed = 1.0f;
float t = speed * Time.deltaTime;
transform.Translate(t, 0, 0);
- Now we can run the game
Making bounds for ball motion
- Make four cubes, and stretch them and position them to be boundaries
- Name the cubes left, right, front, back
- We will use these for our bounds
private static float offset = 0.4;
float left = GameObject.Find("left").transform.position.x + offset;
if (transform.position.x < right) transform.Translate(t,0,0);
Making the emojis
- Go find two or three emoji images from the web, with transparent backgrounds.
- Download these images onto your hard drive in the assets folder of your project.
Making objects of your emojis
- Create a quad
- Position and rotate it to be horizontal
- Create a material called emoji1Material
- Add the emoji1 image as the albedo map
- Change render mode to cutout
- Set alpha cutoff
- Set metallic alpha
- Set smoothness
- Turn off specular highlights and reflections
- Do it again for the second emoji
Making prefabs of your emojis
- In project panel, right click and create a prefab.
- Name the prefab
- From hierarchy, drag the emoji to the prefab in the project.
- Do the same for the other one.
- We will make our current script also control creating emojis
- It will check to see if there are none, and then will add some at random locations.
- First add public variables to the script for emoji1Prefab and emoji2Prefab. These should be GameObjects:
public GameObject emoji1Prefab;
- Then, we can use the Unity interface to set these values.
- We can check to see if the scene does not have any emojis (by looking for the name of the prefabs). If none are found, we can add some more.
if (GameObject.Find("emoji1Prefab(Clone)") == null && ...
if (i%2==0) go = (GameObject) Instantiate(emoji1Prefab);
float x = Random.Range(left, right);
go.transform.position = new Vector3(x,y,z);
Collision detection And removing emojis
- Create a new script called EmojiScript.
- The EmojiScript will actually perform the collision detection.
- Attach this new script to each of the emoji prefabs.
- In Update, we are going to get the ball, and then determine the distance between the ball and the emoji. If it is within some threshold, then a collision has occurred, and we remove the emoji.
GameObject ball = GameObject.Find("ball");
Vector bp = ball.transform.positon; // ball position
Vector p = transform.position; // emoji position
if (Vector3.Distance(bp, p) < distance)
Destroy(gameObject); // destroy the emoji
- We would like to make the emojis grow when they spawn instead of just appear. This involves animating the scale.
- Another thing we might do is make the emojis do something cool when they die instead of just disappearing like move up.