Forum

Coquitlam >> UN401 >> 9:10 am - 10:10 am

11/03/2018 - Jason Ngan
Today, we learned how to create objects! It's one line of code, but rather long.

First, we need to turn your bullet, which we want to create, into what is called a Prefab. A Prefab is like a master copy/snapshot. You'll set-up an object, changing the size, rotation, adding Scripts, etc, but you don't want to do this every time you add this object. Using a Prefab will make a copy of what you did, and allow you to just drag and drop a completed object. We'll need this for your bullet.

To create a Prefab, simply drag your object from the Hierarchy into your Assets folder. You'll see your object in a gray box.

Next, we'll need to create a public variable:

public GameObject bullet;

Instead of "float", "int", or "string", we're using a new type called a "GameObject". Instead of storing an object or text, it stores a Unity object (like a Prefab!)

You should now see a slot in the Unity Inspector. Drag your bullet Prefab from your Assets folder (not the Scene!) into the slot.

Now, we can get to the one line of code:

Instantiate(bullet, transform.position, transform.rotation);

"Instantiate" is another word for "create" or "spawn". We're adding an object into the game.

However, Unity needs to know 3 things in order to add an object into the game:
1. what object?
2. where should we add it?
3. should we rotate it?

With this code, we're giving it the 3 pieces of information, separated by commas:

1. what object? we want the spawn the object stored inside the public "bullet" variable
2. where should we add it? we want to add it where the player is ("transform.position" is code for the current position of the player)
3. should we rotate it? this doesn't really matter for us, but for simplicity, we just said to use the player's current rotation

To make use of this code, you can create a bullet every time you press space!

if (Input.GetKeyDown(KeyCode.Space)) {
Instantiate(bullet, transform.position, transform.rotation);
}

10/13/2018 - Jason Ngan
This week, we learned about variables! Variables hold data. In this case, we'll use them to hold number data in particular. Some examples of number data include: speed, hp, shield, level, etc.

Creating a variable is easy:

float speed = 2.0f;

"Float" is another word for decimal number. "Speed" is the name of the variable, you can name it whatever you like (no spaces though), such as playerSpeed, speed, movement, velocity, etc, or even abc, though that's not recommended since you can't tell what the data means at a glance.

Using a variable is just as easy. Instead of using the number "2.0f", just use the variable name!

if (Input.GetKey(KeyCode.W)) {
transform.Translate(Vector2.up * speed);
}

If you add the word "public" in front of the variable type (the "float"), you can change it in the Inspector. Just be careful: the number you have in the Inspector will overwrite whatever is in the code!

Homework: create 2 more variables to hold number data

Bonus: use a variable by adding two other variables together

10/20/2018 - Jason Ngan
This week, we went deeper into variables! As we learned, a variable keeps track of number data, like the speed or health of the player. To create a variable, you need 3 things, as seen below:

float speed = 4.5f;

1. what data to track ("float")
2. the variable name ("speed")
3. the initial value ("4.5f")

The "float" is another word for "decimal number". You need to add the "f" at the end of the number. However, there are other data types like "int", short for "integer".

int health = 100;

You can print the variable to see what number is stored!

print(health);
print(speed);

Before, we used to print messages like:

print("hi");

But now, we can store a message in a variable too! Instead of "float" or "int", a message is a "string":

string message = "hello UN301!";

And to print, it's the same as using any other variable:

print(message);

Sometimes we need to change the number, like health. We won't be at 100 health forever, otherwise we'd be invincible.

To change a variable, you don't need the data type:

health = 50;

And we can do math operations:

health = health - 10;
speed = speed + 1.5f;

Try to lose health when you press a button!

if (Input.GetKeyDown(KeyCode.H)) {
health = health - 10;
}

Homework:
int health = 100;
What's the difference between:
1. print ("health:);
2. print (health);

Bonus: what is the result of this? (You can print it to see!)
int health = 100;
health = 80;
health = health - 10;
print(health);

10/27/2018 - Jason Ngan
This week, we mostly reviewed last week's content with some time to work on our projects.

We also learned about the "public" keyword. If you add "public" in front of your variable, you can change it inside Unity's Inspector!

It's as simple as adding public to the front. So if you have:

float speed = 5.1f;

Now it becomes:

public float speed = 5.1f;

There is one key idea to remember: whatever number is in the Unity Inspector will overwrite whatever is in the code. If you see something not changing when you modify the code, check your public variables!

09/08/2018 - Jason Ngan
This week, we learned about the Unity editor and began designing our game, starting with sprites.

The Unity editor has a few main parts:
- the Scene, where you add the characters in this level
- the Assets, which is your "inventory" and includes everything from code, to art, to music
- the Hierarchy, which is a list of objects in the Scene for easy access
- and the Inspector, where you control the settings for each object

Adding sprites (images) to your game is easy:
- find a 2D image on Google and save it to your computer
- drag that sprite into the Unity Assets folder to add it to your "inventory"
- then drag that sprite from the Assets folder into the Scene to add it to your game!

You can also change the position, rotation, and size of each object, using the buttons on the top left.

Homework: Find at least 4 sprites - your player, your bullets to shoot, an enemy, and a background

Bonus: What does the Start() and Update() functions do?

09/15/2018 - Jason Ngan
This week, we started learning to code! Programming languages come in many different versions, just like real languages, but some share many similarities, a bit like French and Spanish. The language we're going to use is called C# and a document that contains code is called a Script.

Create a C# Script (right click on the Assets folder -> Create -> C# Script) and name it "Player". Then open it. This should open what's called the code editor (Visual Studio or Monodevelop by default), which is where we edit code, just like how Microsoft Word is used to edit text.

Ignore the top section for now, let's focus on the core pieces of the code. There are two functions you'll find, called Start() and Update(). These are the main engines that keep your game running.

Start() runs once, and it's main goal is to setup your object. Some things the game needs to know are:
- the player's starting health
- the player's starting shields
- the player's starting weapon
- the player's starting inventory items, and so on

Once you setup your character in Start(), the game runs the Update() function, which will run over and over, forever, until your game ends.

Let's try our hand at coding! In Start(), let's write:

print("hello there");

Save the code and go back to the Unity editor. Select the Console tab (usually next to the Assets folder) and run the game. You should see the message pop up! As Start() only runs once, you should only see it once.

Next, let's print another message in Update(). Run the game and you should see it run over and over and over... until you stop the game.

Homework: put two print() statements in Start() and three print() statements in Update(), can you predict what the output is?

Bonus: How can you make a character move?

09/22/2018 - Jason Ngan
This week, we learned about brackets and input! You've probably already noticed, but there are a lot of brackets that come with the code. In particular, we have these strange curly brackets: { and }.

You'll notice that when we wrote the code last week, we went over the Start() and Update() functions. How does the computer know which code to run inside Start() (which runs once) and which code to run inside Update() (which keeps running over and over)?

It can tell because of the { and } brackets! All the code between { and } after Start() "belongs" to that Start() function, and it's the same for Update(). Make sure that they always come in a pair. If you miss one, it'll cause chaos in the code and chances are, it won't work.

Next, we started to learn about input. It's a lot of code, but this is the code to do print "hi" when the "space" button is pressed. We'll put this inside the Update() function:

if (Input.GetKeyDown(KeyCode.Space)) {
print("hi");
}

If you wanted to use a different button, you can chance KeyCode.Space to something else, like KeyCode.A or KeyCode.UpArrow!

Homework: Choose four buttons to print different messages like "moving up" and "moving left". We're going to use them for movement!

Bonus: Try using GetKeyUp or GetKey instead of GetKeyDown. What's the difference?

09/29/2018 - Jason Ngan
This week, we started learning movement! First, we want to go through the difference between button presses:

GetKeyDown(): do something when you press the button down (the first time)
GetKey(): do something as long as you hold it down
GetKeyUp(): do something when you let go of the button

For movement, we'll use GetKey().

if (Input.GetKey(KeyCode.W)) {
transform.Translate(Vector2.up);
}

However, if you want to move faster or slower, you can multiply (using the * key) by the speed you want:

if (Input.GetKey(KeyCode.W)) {
transform.Translate(Vector2.up * 0.5f);
}

You have to add the "f" to the end of any decimal values. We'll go over this later. Multiplying by 0.5 makes you move half speed. Multiplying by 2 makes you move twice as fast.

if (Input.GetKey(KeyCode.W)) {
transform.Translate(Vector2.up * 2.0f);
}

Homework: Add all 4 directions

Bonus: create a variable to hold the "speed" data