Class hours: 9:40 – 2:05
Mr. Bohmann
wbohmann@ewsd.org
Week Thirty!
Today’s Notes
- Today is an EHS A Day and early release
- BFA – early release (Eric)
- Tonight: National Technical Honor Society Ceremony 7:00 pm
- There will be a rehearsal today 9:45 – 10:45am for NTHS students
- Tonight, NTHS students arrive at 6:30pm
- We have three visitors tomorrow – Pixel Art (Is what I’m thinking)
9:40 Karting Exploration – for the rest of us…
- New Project from Template – Karting Microgame
- When project loads – follow the tutorials
- Create new track variations
- Import an .fbx model from Blender
- Try out different game types
- Explore the interface
The goal here is to get you more comfortable in Unity, while adding your own style to an already built game through some custom modding.
10:35 Break
10:45 Variables, Logic and Expressions
BTW – this was the code from yesterday for our paddle. This script was placed on the player paddle. We declared public Class Level variables and one local scope variable. Do you remember which is which?
public float speed = 2;
public float minX; //limit to the left
public float maxX; // limit to the right
void Start()
{
}
void Update()
{
float moveX = Input.GetAxis("Horizontal") * speed * Time.deltaTime;
//the following code will create the limits left and right for you paddle //using both the global variables and the local scope variable moveX above
transform.position = new Vector2(Mathf.Clamp(transform.position.x + moveX, minX, maxX), transform.position.y);
}
Pinball – Let’s level up using variables
You can use variables to do important things within your game. For example, many games will need to keep a score. Let’s add a score keeping variable to the pinball game and have the results printed in a Debug.Log statement.
To begin, you will need to declare a variable for score (what type of variable will you use? and where should you put it?). We want the Score to be displayed when the game begins (so we’ll place a Debug.Log statement in the Start() method.
One last decision – which game object should we create the script for?
We’ll also need to make the variable Public so we’ll put it at the top of the script too
void Start ()
{
// display the current score on start
Debug.Log ("Score = " + score);
}
//score is a great variable name because it is...semantic!
Inside the OnCollisionEnter2D() function, set the score variable value to 1. Then, again use Debug.Log() to display the current score to the screen.
void OnCollisionEnter2D (Collision2D otherObject)
{
// set the score to 1
score = 1;
// display the new score
Debug.Log ("Score = " + score);
}
In the example above, the score just stays at 1. Not entirely what we would want. So, let's look at using some operational shortcuts in our code to how we can calculate the score each time the ball collides with a gameObject that has a collider.
score = score +1 //adds 1 to the variable
score +=1 //same as above, but add what's on the right to the left
score ++ //means add 1 to the score
Instead of using just the Debug.Log function, we could initialize our score variable at the top of our script as a public variable and then as the game plays we can watch the score increase in the inspector.
Let’s Level Up some more: Setting up Score with the UI Canvas – Instruction
Project Details/Deliverables:
- The player’s score should start at 0 when the game begins
- The player’s score should be displayed in the top center of the screen
- Each time the ball collides with (bumpers paddles, walls, the score should increase by 1
- When the game ends, the phrase “Game Over” should be displayed under the score
The big picture – Here is a basic outline of the steps you need to complete the tasks above:
- Add a Canvas and two Text objects (one for the score, one for the game over message)
- Edit the existing BallScript (or whatever you named your script on the ball) and add a score variable and two public Text variables
- Modify the BallScript functions to update the Score and Text variables as needed
- Link the two Text objects to the two Text variables in the BallScript
Easy, right? We can work on the steps together if you are having trouble.
If you get lost, here is a reference worksheet
11:30 Unity – Logical Expressions
Normally, a pinball game like the one we’ve made will only add to the score when a bumper is hit. You may even want to assign different point values to different targets. To support this kind of advanced scoring, you would need to examine the actual objects involved in the collisions and make decisions about how many points to add to the score.
The general process of selecting statements to run is called flow control.
An expression is a part of a code statement that is evaluated to produce some answer. You are already familiar with mathematical expressions like 4 + 4 that will produce an answer such as 8.
int speed = 4 + 4; //score is a mathematical expression
To make decisions in your code, you need a different kind of expression – a logical expression. That means questions you ask in your code have two possible answers – true (yes) or false (no).
So, you remember from our Endless Runner that a Boolean (bool) data type is used to store a true or false value.
bool alive = health > 0; // health > 0 is a logical expression
There is no mathematical operation, but instead, this is a comparison that looks at the value on both sides of the “>” symbol in the middle. The answer is either true or false.
There are several comparison operators:
- == (equal to operator) the result is true if both sides are equal
- != (not equal operator) the result returns true is two sides are not equal to each other
- < (less than operator)
- >(greater than operator)
- <=(less than or equal to operator)
- >=(greater than or equal to operator)
There are several logical operators:
Logical Operator | C# Symbol | Example | Description |
---|---|---|---|
and | && | A &&B | Returns true if both “A” and “B” is true, or false otherwise |
or | || | A || B | Returns true if either “A” or “B” are true, or false otherwise |
exclusive or | ^ | A ^ B | Returns true if either “A” is true and “B” is false, or vice-versa |
not | ! | !A | Returns true if “A” is false or false if “A” is true |
Let’s Practice. If you had two variables named score and health, how would you write a logical expression that was true if both score was greater than 100 and health was equal to 0?
Let’s Practice more….
void Start ()
{
int value1 = 50;
int value2 = 100;
int value3 = 100;
int value4 = 200;
bool result1 = value1 == value2;
bool result2 = value2 == value3;
bool result3 = value4 > value3;
bool result4 = value1 <= value2;
bool result5 = value2 != value3;
bool result6 = result1 && result2;
bool result7 = result1 || (value4 < 300);
bool result8 = !result1;
bool result9 = !(value3 == 100);
bool result10 = value1 + value2 >= 150;
bool result11 = value2 - value1 * 2 == 100;
Debug.Log ("result1 = " + result1);
Debug.Log ("result2 = " + result2);
Debug.Log ("result3 = " + result3);
Debug.Log ("result4 = " + result4);
Debug.Log ("result5 = " + result5);
Debug.Log ("result6 = " + result6);
Debug.Log ("result7 = " + result7);
Debug.Log ("result8 = " + result8);
Debug.Log ("result9 = " + result9);
Debug.Log ("result10 = " + result10);
Debug.Log ("result11 = " + result11);
//Write on a piece of paper 1-11 and declare if the expressions //will return true or false
}
Let’s check our answers….
11:45 Lip Sync Battle Worksession
12:15 Lunch
12:45 Independent Reading
1:10 Break
1:20 Independent Project Worktime of Individual Support
- WorkSession for Lip Sync Project
- 20%
- Help with morning Coding