Unity 3D – Stealth Game – The lift – JS
Load prefab:
-> (parent) ‘prop_lift_exit’
|Box Collider, check ‘Is Trigger’, uncheck ‘Use Gravity’, check ‘Is Kinematic’ (to detect the player position)
|Audio Source, uncheck ‘Play on Awake’, check ‘Loop’ (lift raise noise)
|LiftDoorsTracking.js
|LiftTrigger.js
–> (child) ‘door_exit_inner’
—> (2nd child) ‘prop_lift_collider’
|Mesh Collider (to block the player penetration)
—> (2nd child) ‘door_exit_inner_left_001’
|Mesh Renderer, check ‘Use Light Probes’
—> (2nd child) ‘door_exit_inner_right_001’
|Mesh Renderer, check ‘Use Light Probes’
–> (child) ‘door_lift_exit_carriage’
|Mesh Renderer, check ‘Use Light Probes’
–> (child) ‘door_lift_exit_mechanism’
|Mesh Renderer, check ‘Use Light Probes’
–> (child) ‘door_lift_exit_wires’
|Mesh Renderer, check ‘Use Light Probes’
LiftDoorsTracking.js
#pragma strict
public var doorSpeed : float = 7f; // How quickly the inner doors will track the outer doors.
private var leftOuterDoor : Transform; // Reference to the transform of the left outer door.
private var rightOuterDoor : Transform; // Reference to the transform of the right outer door.
private var leftInnerDoor : Transform; // Reference to the transform of the left inner door.
private var rightInnerDoor : Transform; // Reference to the transform of the right inner door.
private var leftClosedPosX : float; // The initial x component of position of the left doors.
private var rightClosedPosX : float; // The initial x component of position of the right doors.
function Awake ()
{
// Setting up the references.
leftOuterDoor = GameObject.Find("door_exitOuter_left_001").transform;
rightOuterDoor = GameObject.Find("door_exitOuter_right_001").transform;
leftInnerDoor = GameObject.Find("door_exitInner_left_001").transform;
rightInnerDoor = GameObject.Find("door_exitInner_right_001").transform;
// Setting the closed x position of the doors.
leftClosedPosX = leftInnerDoor.position.x;
rightClosedPosX = rightInnerDoor.position.x;
}
function MoveDoors (newLeftXTarget : float, newRightXTarget : float)
{
// Create a float that is a proportion of the distance from the left inner door's x position to it's target x position.
var newX : float = Mathf.Lerp(leftInnerDoor.position.x, newLeftXTarget, doorSpeed * Time.deltaTime);
// Move the left inner door to it's new position proportionally closer to it's target.
leftInnerDoor.position = new Vector3(newX, leftInnerDoor.position.y, leftInnerDoor.position.z);
// Reassign the float for the right door's x position.
newX = Mathf.Lerp(rightInnerDoor.position.x, newRightXTarget, doorSpeed * Time.deltaTime);
// Move the right inner door similarly.
rightInnerDoor.position = new Vector3(newX, rightInnerDoor.position.y, rightInnerDoor.position.z);
}
public function DoorFollowing ()
{
// Move the inner doors towards the outer doors.
MoveDoors(leftOuterDoor.position.x, rightOuterDoor.position.x);
}
public function CloseDoors ()
{
// Move the inner doors towards their closed position.
MoveDoors(leftClosedPosX, rightClosedPosX);
}
LiftTrigger.js
#pragma strict
public var timeToDoorsClose : float = 2f; // Time since the player entered the lift before the doors close.
public var timeToLiftStart : float = 3f; // Time since the player entered the lift before it starts to move.
public var timeToEndLevel : float = 6f; // Time since the player entered the lift before the level ends.
public var liftSpeed : float = 3f; // The speed at which the lift moves.
private var player : GameObject; // Reference to the player.
private var playerAnim : Animator; // Reference to the players animator component.
private var hash : HashIDs; // Reference to the HashIDs script.
private var camMovement : CameraMovement; // Reference to the camera movement script.
private var sceneFadeInOut : SceneFadeInOut; // Reference to the SceneFadeInOut script.
private var liftDoorsTracking : LiftDoorsTracking; // Reference to LiftDoorsTracking script.
private var playerInLift : boolean; // Whether the player is in the lift or not.
private var timer : float; // Timer to determine when the lift moves and when the level ends.
function Awake ()
{
// Setting up references.
player = GameObject.FindGameObjectWithTag(Tags.player);
playerAnim = player.GetComponent(Animator);
hash = GameObject.FindGameObjectWithTag(Tags.gameController).GetComponent(HashIDs);
camMovement = Camera.main.gameObject.GetComponent(CameraMovement);
sceneFadeInOut = GameObject.FindGameObjectWithTag(Tags.fader).GetComponent(SceneFadeInOut);
liftDoorsTracking = GetComponent(LiftDoorsTracking);
}
function OnTriggerEnter (other : Collider)
{
// If the colliding gameobject is the player...
if(other.gameObject == player)
// ... the player is in the lift.
playerInLift = true;
}
function OnTriggerExit (other : Collider)
{
// If the player leaves the trigger area...
if(other.gameObject == player)
{
// ... reset the timer, the player is no longer in the lift and unparent the player from the lift.
playerInLift = false;
timer = 0;
}
}
function Update ()
{
// If the player is in the lift...
if(playerInLift)
// ... activate the lift.
LiftActivation();
// If the timer is less than the time before the doors close...
if(timer < timeToDoorsClose)
// ... the inner doors should follow the outer doors.
liftDoorsTracking.DoorFollowing();
else
// Otherwise the doors should close.
liftDoorsTracking.CloseDoors();
}
function LiftActivation ()
{
// Increment the timer by the amount of time since the last frame.
timer += Time.deltaTime;
// If the timer is greater than the amount of time before the lift should start...
if(timer >= timeToLiftStart)
{
// ... stop the player and the camera moving and parent the player to the lift.
playerAnim.SetFloat(hash.speedFloat,0f);
camMovement.enabled = false;
player.transform.parent = transform;
// Move the lift upwards.
transform.Translate(Vector3.up * liftSpeed * Time.deltaTime);
// If the audio clip isn't playing...
if(!audio.isPlaying)
// ... play the clip.
audio.Play();
// If the timer is greater than the amount of time before the level should end...
if(timer >= timeToEndLevel)
// ... call the EndScene function.
sceneFadeInOut.EndScene();
}
}
Notice: when the lift raises, the player will be child of the lift.