Class hours: 9:40 – 2:05
Mr. Bohmann
wbohmann@cm1985_
Week Thirty Three
Today’s Notes
- Today is an EHS B Day
- CVU students no school on Tuesday – although we’d like to see you.
- Workkeys testing on Tuesday, May 10th and 12th.
- Web Professionals Exam – Tuesday, June 7th
9:40 Attendance
9:45 Monday Mail – let’s see what everyone is saying….
9:50 Agency Presentations
Week 32 Agency Project Dropbox
- What did you work on?
- What did you learn?
- What would you like us to try or know that could be helpful with our own projects?
10:15 Code Review from last week
Let’s apply what we learned in the 3D space and as a review from last week.
10:45 English with Ms. Yopp
11:35 Unity – Working with Data Types/Variables
All of our Game Objects have data attached to them. The Transform of a gameObject has 9 pieces of primitive data – like location of x, rotation of z and scale, etc….
We can store information about game object values to calculate health, score points, among other game play options. Each value can be stored in a uniquely named storage area called a variable.
A variable is defined with a specific data type (such as int or float) and a name such as myScore or player1Health. You can change the variable’s contents while the program is running – that’s why it’s called a “variable”. We’ve seen this in action when we built the Endless Runner game. We used lots of variables to store data.
public class MyScript : MonoBehaviour {
int score; // declare an int called Score
float currentDirection; // declare the float currentDirection
char myInitial; // declare a char called myInitial
string userName; // declare a string called userName
bool isMoving; // declare a bool called isMoving
void Start () {
// numeric data types do not require quotes
score = 8;
// use the "F" suffix to specify a float value
currentDirection = 90.2F;
// use single quotes around individual characters
myInitial = 'A';
// use double quotes around groups of characters
userName = "Will Bohmann";
// boolean values are either true or false
isMoving = true;
}
}
Variable Scope refers to where you place your variables. If you placed your variable in the Start method, it will run when the game begins and then not be accessible again.
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.
void Start ()
{
// display the current score on start
Debug.Log ("Score = " + score);
}
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);
}
Mathematical Operations with Variables
We already know that we use variables to provide more code flexibility. In many cases we may want to store mathematical operations in a variable. Let’s look at some common math operations and learn/review some shortcuts we have been using over the last several weeks.
int added = 3 + 4; // added is 7
int subtracted = 3 - 4; // subtracted is -1
int multiplied = 3 * 4; // multiplied is 12
int divided = 12 / 4; // divided is 3
int remainder = 12 % 5; // remainder is 2
12:15 Lunch
12:45 Focus on Literacy
1:05 Mask Break
1:15 Production Time & Guided Support
Week 33 Agency is now Assigned – Due May 9th