Game Development Reference
In-Depth Information
Illustration 80:
A sliding door with two parts. Arrows indicate sliding direction of each part.
During
Update()
, the script checks the value of
door.IsOpen()
. If this function returns
true
, it means that the door must be open or, if it is not, must be opened. Therefore,
we check
state
, and if its not
SlidingDoorState.open
, then we have three possibilities: the
door is closed (
SlidingDoorState.closed
), being closed (
SlidingDoorState.closing
), or be-
ing opened (
SlidingDoorState.opening
). In the first two cases we have to change
state
to
SlidingDoorState.opening
, and simultaneously send
OnOpenStart
message to inform other
scripts that the door has just started to open. On the other hand, if the state is already
Slid-
ingDoorStart.openning
, then we smoothly move the door towards
slidingPosition
. The smooth
movement is, as we have seen earlier, performed using
Vector3.Lerp()
function. Door
movement has a dead zone of 0.01, after which the position of the door is set to
slidin-
gPosition
. Same steps go in the other direction if
door.IsOpen()
returns
false
, since in that
case the door must be closed. During closing, we keep an eye on
OnCollisionEnter
event.
This allows us to detect anything that might block the door as it closes. A possible reaction
is to reopen the door, or destroy the colliding object. The latter option allows the player to
use the door as a weapon to eliminate enemies.