Hi, everyone. I'm Jeremy Gibson Bond and welcome back to
the Unity Certified Programmer exam preparation course.
In this video, we're going to be looking at part of
the solution to the environmental interactions challenge.
The part we're going to look at here is the desk, the security desk.
So, you can see here that when it's playing,
the security desk sort of pulses,
that green glow around it,
and if I hit the spacebar,
it turns off the security gate.
So, let's look at how this is implemented.
Here in the desk,
I open it up and all of this is attached to
the desk trigger as I showed you in the challenge.
So, there are a couple of different scripts here.
Let's first look at highlight when player in trigger.
So, here, we're going to go ahead and base this on player
interactable rather than having an on trigger enter on trigger exit in this again.
So, we're making use of that player interactable stuff that we already set up before.
So, the first thing I do at the top of the start is I get
access to that player interactable component,
that again we did a required components, so we know it's there.
Then, if I've selected a game object highlight,
I look for these specific values that are on the tune shader.
So, how I know the names of these different fields within the shader?
So, I can look at my game object here,
the desk, and I see that it has the custom toon shader.
Then, I go over to the project and I look for tune,
and I find, "Hey,
the custom toon shader."
Now, here's the key when I select the toon shader over in the inspector,
I get various properties and those properties are the names that I was looking for.
You can see border, border color, outline front.
So, those are the different properties that I've got.
When I go back and look at the shader here, you can say, "Okay,
so border color is probably that and
border size as it appears here is probably underscore border,
and then outline of front is enable outline.
So, these names don't always match up and they almost always don't match up,
but you can usually sort of figure them out by looking
at what names are in the shader itself.
So, once I've selected the tune shader,
I can see the various properties listed and these are
the properties that I can set by setting like a float,
or a color, or something like that.
You see that underscore name which is the name within the shader and then next to it,
the type that it is,
and then how it shows up in the inspector.
So, you can see _Border is border size _BorderColor is
border color and _OutlineFront is enable outline.
Again, you can see these when I click on the desk.
Now, something you probably noticed is that enable outline says it's a float,
but here in the inspector,
it shows up as a checkbox.
That's because a float is a built-in type of value for shaders,
and Booleans really aren't, they aren't really used.
So, it's kind of a way of them faking a Boolean,
is by making it to where the inspector shows it as a Boolean.
Back in the code here,
we've set it to where there's no outline and the size of the border is zero as well,
that's on lines 37 and 38.
Then, down in the update,
if the player is within the trigger according to the player interactable,
then we turn outline front to one,
which sets it to true,
so we are now going to be drawing the outline.
We set the border color to the highlight color that's set in
the inspector for this highlight when player and trigger script.
Then, we set the start time to be the current time,
and set the outline width to be based on a cosine value.
So, you can see it's 0.5 minus the cosine of this,
and what this does is it starts at zero and sort of goes up and down,
and up and down, and up and down.
that will go from zero to one,
to zero to one,
to zero to one,
and multiply that by outline width and that gives us the nice width.
So, then in the update method of this highlight script,
you can see that if according to
the player interactable the player is within the trigger,
we're going to turn that outline front back on.
When we set it to one that's like checking the checkbox.
Then, we set the border color to be the highlight color that it was set too,
and then we set the start time to be the current time.
So, that we don't just start at some random border width,
we start at the lowest border width and then we increase it.
That's what this math is saying here with the set float.
It takes the outline width which is a very small value that it was initially set to,
multiplies it by 0.5,
so it's a half of that initial value,
and then subtracts the cosine of this thing that kind of pulses times 0.5.
So, cosine starts at one,
we multiply it by 0.5 and it's 0.5.
So, then we start by subtracting 0.5,
which starts us at zero,
and then it goes all the way to what would be negative one,
which is now negative 0.5,
which is 0.5 minus negative 0.5,
which is back up to one.
So, what this does is it starts with outline width of zero,
and it ramps it up to that maximum amount line width that we
set initially as the outline width in the desk, and then it goes back down,
and it goes up and it goes down,
and it pulses based on this highlights seconds
per pulse value that is set in the inspector.
Once the player is no longer within the trigger,
outline front is set back to zero,
border is set back to zero,
and start time is set back to negative one,
which will cause it to reset on line 57 the next time the player steps into the trigger.
By the way, this is another reason that player within
trigger is false if there are no actions to take place.
So, once the player has turned off the security gate,
the desk should not highlight anymore.
So, player within trigger appears to be false even though the player is
actually within the trigger because there are no more actions to take place on that desk.
That helps the player to
understand that they've achieved what they needed to do with the desk,
and they can move on, there's nothing else for them to do.
The other aspect of the desk is
this new player action deactivate game object that I asked you to make.
You can see it has one time left to activate.
So, once we've done it once,
it will go away and be removed from the list of player actions.
The game object to deactivate is security gate beams,
and let's see what it does by diving into the code.
Again, these player actions are very simple.
It's just an action that says if the game object to deactivate is not null,
take that game object and set active to false.
That is the same as just going in and unchecking the security gate beams,
which causes them to disappear.
So, that's it for the desk.
Up next, we'll talk about the security camera,
both the animation of the security camera and
the light cone script that allows
light cones to sense where the player is. I'll see you in that video.