Game Development Reference
In-Depth Information
Once the bench has rotated 180 degrees, the
allClear
variable will eventually be set to
true
, so
the bench will once again move. Having been rotated, though, it will have to move on its X axis, or
Vector3.Right.
Left is the negative,
-Vector3.Right.
The bench will have to move to its left unless
you want to reverse the direction it turns in.
“Wrap” the
Translate
line with a conditional to check the state of the
allClear
variable:
3.
if (allClear) {
transform.Translate(-Vector3.right * Time.deltaTime * speed);
}
The
allClear
variable, because it is a Boolean type of true or false, is all you need in the conditional.
The conditional, if you remember, evaluates the contents to see if they return
true
or
false
.
4.
Click Play, and watch the bench's y Rotation in the Inspector or in the
console.
The y Rotation is slightly past 270 degrees when it stops, which gives you a good condition to meet
to set the
allClear
flag to
true
and set the y Rotation exactly to 270 at the same time. While this
may sound simple, rotation is actually fairly complicated. If your bench falls off of the walkway just
as it completes its rotation, feel free to move it forward slightly when you are not in Play mode.
It can be managed using quaternion math, where the direction is a simple vector or direction, or
it can be handled with Euler angles where it is split into x, y, and z rotations. The latter option is
generally easier for most people to deal with, but it comes with several issues, such as order of
evaluation and gimble lock. (That's a good term to Google if you've never heard it before.).
Another problem comes with
setting
a transform value (as opposed to
animating
, which is what you
have been doing so far). In C#, you must create a temporary variable to manage the transform. In
Unity's version of JavaScript, you can set a value directly (e.g.,
transform.position.y = 5.5
), but
apparently it is doing the same process under the hood. Let's add the code and then examine it
closer.
5.
Below the rotation code and above the translate code, add the following:
// adjust the rotation and set the allClear flag if over 270
if(transform.localEulerAngles.y > 270) {
Vector3 rot = transform.localEulerAngles; // create a temp variable to store the rotation
rot.y = 270f; // change the y part of the variable
transform.localEulerAngles = rot; // update the rotation to the temp variable's value
allClear = true; // set the flag to true
}
The two new bits that aren't covered by the comments are the first line of instructions for when the
condition is met. The
transform.localEulerAngles
is how you handle the object's rotation using the
local x, y, z values. Another version,
transform.rotation
, uses quaternion rotation, and under the
hood, that is what Unity uses regardless of which type of rotation you use. The other new bit here
is the Vextor3 type. This is a three-part variable where the component parts (x,y,z) can be accessed
and changed using dot notation. So the first line uses the temporary variable to store the object's
current Euler rotation values, and then the next line changes only the y value. The last line feeds the