Pixels Per Unity determines the size of a sprite compared to Unity’s sizes. An image that is 100×100 would be 1 unit tall in Unity if the setting was 100. 200 would be half the size. 50 would be double the size.

Point (no filter) mode keeps sharp pixelated textures.

Choosing to Override the format for images from compressed 16-bits or truecolor keeps the colors from changing due to effects.

Getting back to Unity:

One of the basic tenets of good programming is DRY: Don’t Repeat Yourself. Functions are one way to keep your code simpler, and write less of it. Other ways to make life easier are variables and loops.

Reduce, Reuse, Recycle: Variables

We’ve already used variables a couple of times in the previous lessons. Variables in Programming are somewhat like variables from algebra. They are both a placeholder in the equation or code for another number or in the case of programming, any kind of data.

Variables in C# are considered “strongly typed.” This means that when you create the variable, you have to specify what kind of data is going to go into it. There are many different variable types, ranging from simple numbers to positions and media. Here are a few of the common ones used while programming for Unity:

int: integers, or whole numbers. ex: 1, 2, 3, 4… and so on.

float: floating point numbers, or numbers with decimal points. Used for speed, position, time, or anything where you need finer control than whole numbers. In c#, there needs to be an f after the number. ex: 0.5f 1.3f, 101.23f, and so on.

bool: short for boolean operator, this can only be true, or false.

Vector3 and Vector2: these are groups of floats most commonly used for positions, but can also be used for rotations and scaling. ex: Vector3:(1, 1, 1).

GameObjects: this is a Unity GameObject, and we’ve already used it before for our ship’s bullets so we could fire them.

Here’s how you declare a whole number counter, which is useful for scores, level numbers, and counting a specific number of objects:

int numberVariable;
nunberVariable = 10;

Let’s go back to our ship.cs script and make some changes to learn how to use variables:

First we add the moveSpeed variable to the script. You may have figured out that we’ve specified that it’s a float. And just like our bullet, we’re making this variable public so that we can see and adjust it in the editor Inspector window.

We’re also replacing the 10 from the movement code with our new variable. After saving our script, we need to head into the inspector window in the Unity editor. The moveSpeed variable will appear in the ship.cs script component. It will say 0 because it was just declared, but we can adjust that to 10 just like we had it before.

This is very powerful and useful for a variety of applications. We could create a powerup that will increase speed by adjusting the moveSpeed variable. We could change the moveSpeed during testing to make sure the speed and feel are good.

On an unrelated note, I’ve added Space.World at the end of the Translate method, because it will ensure the ship moves left and right even if the ship gets rotated by accident.

Repeating actions a set number of times: For Loops

Another way to save yourself the time of writing code over and over again are

for (int x = 0; x < 10; x++) {
// your code to repeat
}

Let’s break down the anatomy of the for loop:

int x = 0 is the declaration of the variable that we’re going to count.x < 10 is the condition that needs to be true to continue the loop.x++ is increasing the variable x at the end of each loop by 1. ++ is a shorthand for increasing a variable by 1, and is the same as typing x+=1.
This is how the for loop works if we write it out: Start the count from 0. The following code will be performed while the count is less than 10. The count goes up by 1 each time the code is performed.

Let’s make a smarter enemy

Our basic cubes can be our dumb enemies, but we should throw one or two enemies in the game that are smarter. Create a new 3D shape–in my case I used a cylinder for variety, but this can be another cube or a sprite that we create, and apply a new material to. Name this shape “Commander”, and add a RigidBody component as we did in session 2 with the other enemies. Under Add Component, scroll down and click “New Script”, and call it “commander”. Double-click the new script called commander.cs in the Project window to open it up in the code editor. We’re going to add the following highlighted code:

Here we’re combining a couple of things. By declaring a variable for the Transform component, when we add the ship object to the commander.cs script component in the Inspector, we’re going to get a shortcut directly to the Transform component in our scripts.

In the Update function, there are two conditional statements that mirror each other, using movement code from our ship. If the ship’s x position is more than a fraction to the left or right of the x position of the player, it will move closer to the player’s direction.

Making the enemy shoot

To make the enemy shoot, I’d added a few lines to the commander.cs file. I also created an enemy bullet by copying both the original bullet and its script, creating an enemy bullet that travels down instead of up, and changing the tag to “enemy.”

Moving the enemies

A Coroutine is a side process that runs at the same time as other functions, including Update. Coroutines in C# are also a way to use “WaitForSeconds()”, which is a useful function that lets you set a delay for actions to happen.

In the following code added to the enemy.cs script, I’ve added a few variables for movement, and made them public so they can be adjusted in the Editor’s Inspector window. Coroutines have to be called a special way, by entering the name in a StartCoroutine() function.

In this coroutine, a For Loop is used to repeat a series of actions 10 times. Each step is a delay, followed by moving the enemy in a specified direction. This method for scripting movement also uses a For Loop, starting with the specified distance for movement, and subtracting from it each frame by the fraction of a second that has elapsed, and then moving on to the next step.