Coquitlam >> UN402 >> 11:30 am - 12:30 pm

12/02/2018 - Jimmy Randa
Hi everyone!

Today we had a work week to finish up our enemies in our game.

Have a good week!

12/09/2018 - Jimmy Randa
Hi everyone!

Today we made a checkpoint system in our game, along with respawning when we die.

First we made a checkpoint game object, add a box collider 2D to it, make sure it’s trigger, and tag it as checkpoint.

Place these around your map to act as a checkpoint, once we touch them we will respawn at their position when our player dies.

Next, open up our player script and add the following variable to the top, along with all your other variables:

public GameObject checkpoint;

Next, we changed our death code a little bit. Right now we have it set up so that when we die, we delete our player from the game. To make it simpler, we can just set our position back to the checkpoint. If you’d like, you can delete your player, make a menu pop up with a “retry” button that you have to click to be respawned.

So, inside of update where we have our death code, change it to look like this:

If (Health < 1) {
transform.position = checkpoint.transform.position;
Health = 3;
Score = Score – 100;

We need to make sure we set our player’s health back to 3, well, above 0, because if they’re health is constantly 0 it will infinitely set our position back to our checkpoint. Then, I subtract some score as a penalty for dying.

Lastly, we have our checkpoint variable, but we don’t update it when we touch our checkpoints yet. Lets go to the section of our code that detects when we touch other box colliders:

void OnTriggerEnter2D(Collider2D collision){
if (collision.gameObject.tag == “Checkpoint){
checkpoint = collision.gameObject;

There, now our checkpoint variable is updated, with the checkpoint we collided with.

That was it for today, have a good week!

12/16/2018 - Jimmy Randa
Hi everyone!

Today we had a work week to polish up our games before Christmas break!

I hope everyone has a great holiday!

11/04/2018 - Jimmy Randa

In this class we added a script to our camera so it'd follow the player. We used one of unity's built in functions called “SmoothDamp” which allows us to have a smooth camera as it follows us with a little bit of delay.

First of all we created a new c# script, named it “CameraScript” and attached it to our main camera in the hierarchy. We needed to create a few variables in order for us to pass to the function's parameters.

Sometimes you may not know everything about how to code a certain thing in Unity, and a great way to learn is from Unity’s website. They have awesome instructions on how to use everything. I used the SmoothDamp function as an example, linked here:

As we can see here, the function requires several things to be passed to it

Current: The current position.
Target: The position we are trying to reach.
CurrentVelocity: The current velocity, this value is modified by the function every time you call it.
smoothTime: Approximately the time it will take to reach the target. A smaller value will reach the target faster.
maxSpeed: Optionally allows you to clamp the maximum speed.

So, the current position will just be the current position of the Camera. If you think about it, we're constantly trying to get the camera from where it is, to where the player is. So the current position will always be where the camera already is, and the target position is the player.

Then for currentVelocity, it wants a Vector3 passed by reference, so that's why in our code we just made our variable

Vector3 velocity =;

And then used that in the function. Think of this is a placeholder for where the function can change velocity.

Lastly we only needed to make a variable for smoothTime, which we made as a float and set to .15, like so:

public float smoothTime = .15f;

We can then go to the update section of our script, and create our SmoothDamp function. So since update is called once per frame (Every 0.016 seconds) it'll be constantly running our code to catch up our camera to the player. So our code looked like:
void Update () {
transform.position = Vector3.SmoothDamp(transform.position, GameObject.Find(“Player”).transform.position, ref velocity, smoothTime);

First, transform.position is referring to the camera's position. Every game object's position is found under transform, which is why we do transform.position. We then make it equal to our function, the SmoothDamp(), and inside the smooth damp, you can see us passing all of our parameters: Current Position, Target Position, Velocity, Smooth Time.

Now our camera should be moving smoothly towards our player, and if you'd like a larger or smaller delay, change your smoothTime variable!

That was it for this class, next class we’ll be adding spikes and enemies to our game!

11/18/2018 - Jimmy Randa
Hi everyone!

Today we worked on adding some spikes to our game!

First we found an image of some spikes on the internet that we wanted to use in our game.

Then, we dragged it into our assets folder to put it into unity.

Then we added it to our scene by dragging it into our scene from the assets.

Now we needed to add a box collider to the spikes and tag it as “Spikes”

Lastly, we just needed to add some code so that when we get hit by the spikes, we lose health. This code went among all of our other “void” codes, and it looked like:

Void OnCollisionEnter2D(Collision2D coll){
If (coll.gameObject.tag == “Spikes”){
health = health - 1

Remember to make sure that the word you put in the quotes, like “Spikes”, matches the tag you made for your spikes. Capitalization matters too.

Next we created the knock back so that when our player gets hit by spikes or an enemy, he’ll be pushed away.

Similar to how we use a vertical force to make our player jump in game, we can combine a vertical and horizontal force to make our player get pushed back.

We opened our player script, and added a new function near the bottom. It looked like this:

void KnockBack(Vector3 HitBy){
float XPower = 15f;
float YPower = 15f;

if(transform.position.x – hitBy.x > 0){
GetComponent<Rigidbody2D>().AddForce(new Vector2(Xpower, Ypower), ForceMode2D.Impulse);
} else {
GetComponent<Rigidbody2D>().AddForce(new Vector2(-Xpower, Ypower), ForceMode2D.Impulse);

So what we made here was a simple if statement to determine which direction we should get knocked back.

For example, everything in our game has a center, of course. And we can either be to the left, or to the right of the center of any game object.

All we’re doing in the if statement, is checking if we’re a bit to the right of the center, then get knocked back to the right, else, go the other way (If you notice, the Xpower is negative in the second statement).

We spent the rest of class finding an image to use for our enemy, and got it ready. We added it to our scene, and gave it a rigidbody2D and a BoxCollider2D.

Next class we’ll work on getting enemies that move back and forth, and we’ll be able to jump on them to destroy them, or we can get hit by them and get knocked back.

11/25/2018 - Jimmy Randa

Today we worked on our enemy who moves back and forth.

First, make sure that we added a box collider 2d to it, as well as a rigidbody 2D. Then, create a tag for the enemy named “enemy”, and make sure to give it the tag once you’ve made the tag.

Then, lets create a script for our enemy, name it “EnemyScript”.

Our enemy will work very simply. We place it in our scene somewhere, as a starting position, then it will move left of that starting position 5 units, then it will move right until it reaches 5 units to the right of the start position, then left again, and so on.

In our script, we have to create a few variables first:

Vector3 startPosition;
public float speed = 2f;
public bool goLeft = true;

Our code looked something like:

Void Update() {
If (goLeft == true) {
transform.Translate(new Vector2 (-speed, 0) * Time.deltaTime);
if (transform.position.x < startPosition.x – 5 ){
goLeft = false;
else {
transform.Translate(new Vector2(speed,0)* Time.deltaTime);
if (transform.position.x > startPosition.x + 5 ){
goLeft = false;

Make sure to attach the script to your enemy and test if it moves.

Next, let’s open up our player script, and find the spot where we did the knockback for spikes. Lets add to it an if statement for what happens if we hit something with the tag “enemy”:

If(coll.gameObject.tag == “enemy”){
Health -= 1;

Now, let’s make it so that we can stomp on the enemy’s head and kill it, similar to how in Mario games you can stomp on the mushrooms.

Let’s add a second box collider to our enemy, and position it so that it’s just above the enemy, and a little smaller on the width of the enemy. Make sure to check the “Is Trigger” box for this box collider.

Let’s open up our player code and add some code to the void OnTriggerEnter2D function, this is where we wrote some code to pick up coins before. The whole thing should look something like this:

Void OnTriggerEnter2D(Collider2D coll){
If(coll.gameObject.tag == “Coin”) {
Score += 1;
If(coll.gameObject.tag == “enemy”) {

Lastly, we made it so that we bounce off their heads instead of just falling through them. We created a bounce function and called that function in the OnTriggerEnter2D if “enemy”. The bounce function looked like this:

void Bounce(){
float yVelocity = GetComponent<Rigidbody2D>().velocity.y;
if (yVelocity < 0){
yVelocity = -yVelocity;
Vector3 force = new Vector3(0, yVelocity * 2, 0);
GetComponent<Rigidbody2D>().AddForce(force, ForceMode2D.Impulse);

That was everything for today, next class we’ll work on respawning / checkpoints.

In the upcoming classes we’ll be doing things like adding a way to win our game, a level selector, audio, and much more.

10/14/2018 - Jimmy Randa

Today we finished our player’s animations and wrote some code for them to work!

Last class we finished splitting our sprite sheet up into separate images. We can now use the separate images with animation.

At the top left of unity, there’s a button labelled “Window” if we click on it, we’ll see a whole bunch of options. We’re interested in one called “Animation” click on it and a box will pop up.

We need to select our player in the hierarchy, then a “Create” button will pop up in our animation window. Click create, and give the animation a name, we will be making three, idle, run and jump. Then click save. The window will change a little, and we’ll see our time bar on the right where the create button was.

For our idle animation, we only used one image of my duck character standing still. So I dragged that image from the assets from the bottom, to 0:00 in the animation window.

Next, we needed the run animation. We need to make a new animation, so on the top left, where it says our animation name “Idle” click it, and there is an option for “Create new clip”. It will ask for a name again, “Run” then click save. Now we had 2 images for our run animation, so I dragged the first one to 0:00, the second to 0:10, then the first again to 0:20.

Lastly, our jump animation was just one image again. Create a new clip again, this time named “jump”. Drag our jumping image to 0:00 once again, and we’re done creating animations.

Now, go to Window at the top of unity again, and open the Animator. (Make sure your player is still selected in the hierarchy)

We should see a box for each of our animations. One labelled Idle, one run, and one jump. I arranged mine in a triangle formation. Idle at the top, Run just down to the left, and jump down to the right. We need to make transitions between each of these boxes.

Right click on Idle and select “Make Transition”, then click on Run. Right click on idle again and do the same thing but this time select Jump. You should now have an arrow going from Idle to Run, and from idle to Jump.

We need arrows going back too, so right click on Run and Make a transition to Idle. Right click on Jump and make a transition to Idle.

The last transition we need is from Run to Jump.

Now at the top left of the Animator window, there’s a button called “Parameters”. Click it, and there should be a + Icon just a bit to the right. Click it, and click bool. Name this one Run. Make another called Jump.

Now, click on the arrow going from Idle to Run. Your inspector window should change to see the arrow’s settings.

One is called “Conditions”. It says “List is Empty”, so we need to add a new one by clicking the +. This one should be Run in the first box, then true in the second.

Click on the arrow from run to idle. Find conditions in the inspector again.

Add a new condition. This one should be run false.

Do the same for both Jump arrows. To jump should be Jump true. From jump to Idle should be Jump false.

For our last arrow, the one from Run to Jump, should be Jump true.

Additionally, for every single arrow, we unchecked the box called “Has Exit Time”. Then we clicked on “Settings” still in the inspector window, and unchecked the box called “Fixed Duration”

We’re all done setting up animations, now just code.

In our player script, where we did the movement code we added lines that looked like:

if (Input.GetKey (KeyCode.D)) {
animator.SetBool ("Run", true);
GetComponent<SpriteRenderer> ().flipX = false;
if (Input.GetKeyUp (KeyCode.D)) {
animator.SetBool ("Run", false);

if (Input.GetKey (KeyCode.A)) {
animator.SetBool ("Run", true);
GetComponent<SpriteRenderer> ().flipX = true;
if (Input.GetKeyUp (KeyCode.A)) {
animator.SetBool ("Run", false);

And our old jumping code was changed a little to look like:

void OnCollisionStay2D(Collision2D collider){
if (collider.gameObject.tag == "Ground") {
canJump = true;

void OnCollisionExit2D(Collision2D collider){
if (collider.gameObject.tag == "Ground") {
canJump = false;

That was everything for today! Next class we’ll finish this up because not everyone finished, and we’ll have some time for things like designing a big level for us to play on.

10/21/2018 - Jimmy Randa
Hey everyone!

Today we made an animated coin in our game, along with the ability to pick it up and earn score.

We made a score variable and we learned about box collider is trigger.

To start, we took our coin sprite and went to the inspector window. We changed “sprite mode” to multiple, then we opened the sprite editor. We used the split button to split it into 6 columns and 1 row, then we adjusted the size of the boxes for a nice fit around each coin image. When we were done we clicked apply on the top right, and closed the sprite editor.

Next, we put the first coin image into our game, we dragged it up from the assets into our scene. We added a box collider 2D to it, and check marked “Is Trigger”.

Lastly, we made a tag for our coin game object, named “Coin”.

Then we got to work on the animation. Open our animation window again, by going to the top of unity and clicking “Window” “Animation”. Select our coin game object in the hierarchy, and a create button should be on the animation window. Create an animation, and we named it “Coin” then save.

Now to animate our coin, we need to drag each image one after the other into the time bar. I chose to drag one in every 10. I started with the first image on 0, the second image on 10, third image on 20, and so on. We had 6 images so we ended at 50. Try clicking play on the animation window to see your coin animate in your scene!

Then we opened up our player script and added the following code, just under our no double jump code:

public int score = 0;

void OnTriggerEnter2D(Collider2D coll){
if (coll.gameObject.tag == "Coin") {
score += 100;
Destroy (coll.gameObject);

Now, we have a score variable to keep track of our score, however we don’t show it in our game anywhere. See if you can pick up your coin in game and then click on your player in the hierarchy, and the score variable should have gone up from 0.

Lastly, we made a prefab out of our coin so we could add more of them into our game. Drag the coin from the hierarchy into our prefabs folder.

10/28/2018 - Jimmy Randa

Today we made that GUI to display the health and score while we play.

Come to your Unity hierarchy, and right click, select “UI” and click “Canvas”. Canvas should now be in your hierarchy, along with EventSystem. Make sure you don’t delete the EventSystem as it’s important for the GUI to work!

Then we right clicked on our Canvas, Clicked “UI”, Clicked “Text”.

This makes a text box in our GUI. We changed what the text said to “Health: ”, and we resized it a bit, and moved it up in the top left corner.

In our inspector, we changed the anchor to the top left of the screen, and then we checked the box called “Best Fit” near the bottom. The anchor and best fit options make sure that our GUI is the right size no matter how big or small our screens are, and also in the right position.

Duplicate your text, move it to the top right and change what it says to “Score: ”. Also change the anchor to top right.

At this point, I think it’s a good idea to rename our Text in the hierarchy to something more specific. Name the Score one “Score” and the Health one “Health”.

Now let’s go back to our player script and add some code that will update our texts with the numbers in our variables we made from previous classes.

We need to make 2 variables that will hold those text boxes we made, but in order to do that we need to import some new code into our script.

Using UnityEngine.UI

Our script editor doesn’t come with everything automatically, to save space, so if we want to use GUI related code we have to do what’s called “importing” it with that line of code. So now we can make these two variables:

public Text healthText;
public Text scoreText;

And lastly, inside of update we simply need to add somewhere:

healthText.text = health;
scoreText.text = score;

And that was it for this class! Next class we’ll be looking at making our camera follow our player.

09/09/2018 - Jimmy Randa

Welcome to the new semester! My name is Jim and I'm your instructor for Unity 302. I'll be using this page to post summaries of what we learned in class, it'll include all the scripts and code we went over too. I'll also be posting optional homework / bonus assignments that you can do at home!

Make sure you’re also bringing a USB drive to class!

Today we went over some review, we went over the unity work space, how to stay organized, and a few keyboard shortcuts.

The workspace goes something like this:

• The hierarchy contains everything that’s in our scene – it keeps track of everything we put into our game, also known as “Game Objects”.
• Scene view is where we can edit out games levels
• Game tab shows us what our camera can see. This is what the player sees when playing their game.
• The inspector shows us properties of a selected Game Object.
• The asset window is where we keep all of our assets nice and organized! Anything in this window can be added into our scene. Just remember that if something is in the assets window, it does not necessarily mean it’s in our game yet!
• The console tab shows us errors in our code, or things we print from code!

It’s important to stay organized so that we can find everything easily! In the asset window at the bottom, we created folders by right clicking in the grey space, selecting create and selecting folder. We made ones named “Sprites”, Scripts”, “Animation” and “Scenes”.

We then added an image to our project, by dragging it into our Sprites folder. Then we dragged it into our scene, which turned it into a Game Object in our hierarchy. The image may have had a strange name, so it’s important that we rename our Game Object to what they are. You can do this by selecting it in the hierarchy, and at the top of the inspector there is a box to change its name.

The keyboard shortcuts were as follows:

• Q to use the pan tool, pans the screen.
• W for the move tool, lets us move the selected object.
• E allows us to rotate objects, make sure you're not rotating the sprite on the Z axis because we are in 2D!
• R is the scale tool, for resizing things bigger or smaller.
• T allows us to move our objects freely and scale our objects.

Next class we will be going over an introduction to scripting!

09/16/2018 - Jimmy Randa
Hey everyone!

In our second class we learned / reviewed about some coding concepts

We went over a few things, like the difference between Start() and Update(), curly brackets, semi-colons, and if statements.

The code we put inside of start runs once when the game starts.

The code we put into update runs repeatedly while our game plays. Specifically it runs 60 times a second, because Unity runs at 60 frames per second.

We learned a little bit about variables, which had a few things to them:

Public or not? Public is an optional word you can put at the start of your variable. If you use public, it means Unity can see the variable and display it to you while the game is running! Very useful for trying to change things while playing your game to find the best settings possible.

A data type. A variable needs a data type because it needs to know if it’s going to store a number, or a word, or whatever else. There are many data types, a few we looked at were int, string, and float. Int means a number without decimals. String means letters (or even words / sentences) and float means a number with decimals! You cannot mismatch these, for example if you make a string variable, it cannot have numbers in it.

Lastly, your variable needs a name. This can be anything, but it’s a good idea to keep variables named after what they’re doing. For example speed, or health are good names that control player speed, and player health.

The semi colon is something we need after almost every line of code. Your code will have either semi colons, or curly brackets. If it has one, it does not need the other. Semi colons go after every single line, whereas curly brackets go after if statements, functions (like start or update!).

An “if statement” in programming is basically saying you’ll run the code inside, if the condition is true. For example in real life we might say: “if it’s sunny today, I’ll go play soccer outside”.

For next class find a few sprites that you want to use in your game. Ground sprites, spikes, backgrounds, players, or decorations. If you can’t find a good sprite sheet for your player I can provide you with mine in class! Remember to search for transparent images!

09/23/2018 - Jimmy Randa
Hey everyone!

In our third class, we started by adding images to our Unity projects, so we could use them for platforms, and our character. We then went on to get started with movement code, and we looked at what box colliders and rigid bodies do.

Add an asset into your project by dragging and dropping it into your Unity’s assets section at the bottom. Make sure to put all your new images into the sprites folder, to stay organized!

To start, we dragged in one image of ground to our scene from our assets, making one Game Object in the hierarchy.

You may have to rename this Game Object to something like “Ground” to stay organized. You can rename your game objects at the top of the inspector window when you have them selected.

We found a player to use in our game and put it into our scene, above the ground.

We began by learning the different between what a rigidbody 2D does, and a box collider 2D.

• We added a rigidbody 2D to our player, and we noticed that it began to fall down, right through our ground! We add a rigidbody to whatever we want to have physics, and have gravity!
• To stop our player from falling through our ground, we need to add what’s called a box collider 2D. Both your player and your ground will need one. We added these and saw that our player could now be stopped by our ground, and was left standing on top. A box collider is added to anything you want to have collision with! Remember, both things need a box collider for the collision to work (for example both the player and the floor)

Then, we got started on making our player move and jump! Our player will be able to move left, right, which will be very similar to UN301 if we remember, however jumping will be unique. We’ll use physics and gravity for a very nice jump!

We came to our scripts folder in the assets, and created a new script called “Player”

Open it up and we added the following code:

public float speed = 5.0f;
public float jumpForce = 5.0f;

void Update () {
if (Input.GetKey (KeyCode.A)) {
transform.Translate (new Vector2(-speed,0) * Time.deltaTime);
if (Input.GetKey (KeyCode.D)) {
transform.Translate (new Vector2(speed,0) * Time.deltaTime);
if (Input.GetKeyDown (KeyCode.W)) {
GetComponent<Rigidbody2D>().AddForce (Vector2.up * jumpForce, ForceMode2D.Impulse);

Note how our A button uses negative speed, because we're going backwards (negative) on the X axis, and D uses positive speed, because we're going forwards (positively) on the X axis.

The new Vector2() that we're creating simply takes an X and a Y as numbers, so since we only want to control movement left and right with these keys, the Y number is 0.

At this point, make sure to save your script so Unity gets updated with your changes, and we can now attach the script to our player object by dragging it onto it, and you should be able to now play your game and move your character left, right and up.

One problem with our game though is that our player can jump while already in the air! This will be fixed later on.

09/30/2018 - Jimmy Randa
Hello everyone!

Today we fixed the jumping while in the air issue, and we got started on animation.

To fix the jumping issue we first tagged our ground in our hierarchy as “Ground”. Then we came to our code and we added a few parts:

bool canJump;

void OnCollisionStay2D(Collision2D collider){
if (collider.gameObject.tag == "Ground") {
canJump = true;

void OnCollisionExit2D(Collision2D collider){
if (collider.gameObject.tag == "Ground") {
canJump = false;

Remember, whenever we’re adding these “void …” parts (Called functions) you need to make sure that you are not adding them inside of another function’s curly brackets. Make sure that you’re adding it outside of update’s curly brackets (But still above the very last one as that closes our entire script)

Then we changed our if statement that controls jumping to look like this:

if (Input.GetKeyDown (KeyCode.W) && canJump)

This means that both the W key has to be pressed, and canJump is true before running the code inside this if statement, which is the code that makes us jump.

We then got started on animations by slicing up our player sprite sheet.

Click on your player sprite sheet image in your assets sprites folder. The inspector window should pop up with its settings. We need to change “Sprite mode” to multiple, then click on the “Sprite editor” button. Click apply on the box that pops up, and we should see our sprite editor window.

At the top left, there’s a button called “Slice”. Click on it, then change type to “Grid by Cell Count”. Our player has 1 row of sprites, but 4 columns, so we need to put those numbers in. Then click slice. There should now be a box for each image.

We need to manually resize each box to have a tight fit around our sprites, so it looks nice in our game. Once you’re done click apply at the top right, and we can close the sprite editor.

Next class we’ll be animating these images and adding code so that animations change as we do different things in game!