I've modified BeginState
quite a bit just for the timer. There are now three examples of ways to switch to PlayState
. Plus, there's a timer countdown displayed in the upper right corner. I also put code that I had to write more than once into a new Switch()
method.
BeginState
as shown in the following screenshot.I'm not going to walk you through the State switching code, I already did that previously.
An analysis of the code shown in the preceding screenshot is as follows: On the BeginState
class
For lines 9, 10, and 11:
Line 16: futureTime = screenDuration + Time.realtimeSinceStartup;
Time
class is a Unity classrealtimeSinceStartup
float
type of value (decimal number)realtimeSinceStartup
and is added with the value in the screenDuration
variablescreenDuration
variable was declared on line 11 with a value of 8 seconds, which is the length of time BeginState
will be active before switching to PlayState
futureTime
futureTime
variable was declared as a float
type on line 9Line 17: Time.timeScale = 0;
0
to the timeScale
variable causes time in the game to standstill, thus pausing the gameLine 22: float rightNow = Time.realtimeSinceStartup;
rightNow
is declared and assigned the current elapsed timefutureTime
with a value of 8 seconds into the future, and rightNow
with a value of the actual seconds right now, which is updated constantlyStateUpdate()
method which is called constantlyLine 23: countDown = (int)futureTime – (int)rightNow;
countDown
which was declared on line 10rightNow
is constantly updated, so in 8 seconds the values in futureTime
and rightNow
will be equal to each othercountdown
starts at 8 and decreases to 0, in 8 secondsNotice the (int)
in front of each variable. The two variables are of type float
, while countdown is of type int
. I want it to be of type int
when used later in line 38. The (int)
type takes the float
value, strips off the decimal part of the number, and leaves just an integer. A float
is being cast (changed) to an int
.
Line 25: if(Input.GetKeyUp(KeyCode.Space) || countDown <= 0)
OR
operatorcountDown
reaches 0, this condition becomes truePlayState
Line 27: Switch();
Switch()
method on line 41switch()
methodLine 43:: Time.timeScale = 1;
1
to timeScale
0
on line 17Line 44: manager.SwitchState (new PlayState (manager));
Line 27: }
The following is the code flow of BeginState
GUI:
Line 33: if (GUI.Button(new Rect(10, 10, 150, 100), "Press to Play"))
GUI.Button
in the Scripting Referencebool
) of true when clickedif
statement to detect when it's clickedButton()
method has the following two parameters:Rect
object (rectangle) is created which specifies its position on the screen and its sizestring
text that will be on the buttonLine 35: Switch();
Switch()
method on line 41Line 38: GUI.Box (new Rect (Screen.width – 60,10,50,25), countDown.ToString());
Box()
method creates a visible rectangle box in the Scene to display the timer counting downScreen.width
provides the pixel position of the right side of the Scene. Then I moved 60
pixels to the left of the right edge of the Scene.60
mean: from the last pixel on the right, come back 60 pixels to the left, and that's where to start drawing the rectangle box.countDown.ToString()
method takes an integer value stored in countDown
and converts it to a string
value.Box()
method requires a string
type of value.Considering how much is going on in the Scene, and that there are three ways to change to PlayState
, there really isn't very much code in BeginState
.
When you know the syntax, how to use variables and methods, and the concept of working with objects, coding is actually quite simple to learn because you're doing the same types of thing over and over. The most difficult part of coding is trying to discover all the features that Unity provides in the Scripting Reference, and how they work.
You could get to know C# to the point where you dream in code, but then you have to apply that coding knowledge to Unity. So you look in the Scripting Reference and think what is a EulerAngle or a Quaternion? What takes time is doing the research, and not necessarily writing the code.
13.58.44.229