09: A Mazing Puzzle (Part 3)
Learning Target
- Add collision triggers
- Use scripts to react to collision triggers
Resources
Door Models:
Starter Scripts:
Optional Resources:
Instructions
Step 1:
Two door models have been provided. You will need to:
-
- Import the models into your Models folder
- Create original prefabs of the doors
- Assign materials to the doors
- Add a collision box to prevent passage by the player
Then, find 2 or 3 spots to locate doors in your maze and add them to your scene.
Step 2:
It will look a lot better if our doors open, rather than just disappear. To do this, we'll create a small C# script (or use the template above).
Note: class-level variables are called several different things in different contexts. Here are some commonly used terms:
-
- class attributes
- member variables
- fields
Your script will need these public member variables so you can make adjustments:
-
- IsOpen (boolean)
- Speed (floating point)
- OpenHeight (floating point)
You'll also need these two private fields to keep track of positions:
-
- upPosition
- downPosition
Optional:
You could use a public variable to control which direction your door moves when it is opened (e.g. up, down, left, right):
- OpenDirection (Vector3)
You could make your door swing open instead of sliding. This is a bit more of a challenge.
Step 3:
When the game first starts, we need to store both the up and down positions for that specific door. To store the "up" position, we just need to move the starting position along the y-axis. For example,
upPosition = this.transform.position + new Vector3(0, OpenHeight, 0);
We can slowly move from our current location to a destination position using the MoveTowards() method. This is not complete code, but shows what we are trying to do:
this.transform.position = Vector3.MoveTowards( current position of door , target position of door , distance to move this frame);
You might remember from math and science that distance = speed • time.
We already have a variable for Speed. The time would be how long has passed since the last frame update. Unity provides this information with the variable Time.deltaTime. So, the distance we need to travel can be calculated like this:
distance to move per frame = Speed * Time.DeltaTime;
We also need to use a conditional (if statement) for this to work properly in both directions.
if(IsOpen) {
// move to the up position
} else {
// move to the down position
}
Step 4:
We need a way to tell the doors to open.
Create a Prefab Variant for one or more of your maze blocks. Add a new collision box in the player's pathway.

Change this collision box to be a trigger, but be sure to keep the others as regular colliders.

Step 5:
When the player hits a door trigger, we need to actually control one of the doors. Create a new C# script (or use the template above). The script will need at least two public variables:
-
- Door (DoorMechanism)
- IsOpener (boolean)
- Optional: IsReversible (boolean)
You'll need to add a new function:
void OnTriggerEnter(Collider other) {
}
This function will execute whenever the player moves into the trigger collider. You will be able to tell the door to open by doing something like this:
Door.IsOpen = true;
However, you should only open the door if that is the script's current purpose. Otherwise, you should close the door.
IsReversible?
To make the door's operation reversible, we can use a "flip-flop" code structure in ActivateDoor.cs. It would look something like this:
if(IsReversible) {
if(IsOpener) {
// make this activator a closer
} else {
// make this activator an opener
}
}
Don't forget to add an else statement for when the activator is NOT reversible.
Challenge:
Though it is not required, you are welcome to use the Toggle Switch and its corresponding script to add variety to your maze. It may also give you some ideas on other ways to add interest to your maze.
Grading
For grades A to D, all requirements need to be met for that grade band.
| Doors and Door Activators | Letter Grade |
|
A+ |
|
Meets these criteria:
|
A |
|
Meets these criteria:
|
B |
|
Meets these criteria:
|
C |
|
Meets this criteria:
|
D |
| Doesn't meet above criteria | Reassigned |