Unity 3D – Stealth Game – Laser Grids

Laser Grid

1. Put the 3D ‘Laser Grid’ mesh inside the Scene, it needs:

– ‘Add component’> Physic> Box Collider> check ‘As Trigger’ to detect collision with the player

– ‘Add component’> AUdio> Audio Source to sote the SFX laser effect> check ‘Play an Awake’, check ‘Loop’, 3D Sound Settings> Min Distance> 1.5 (you will listen the SFX if the player reaches 1.5 unity of distance from the Laser Grid)

– ‘Add component’> Rendering> Light, to create a light effect, setup
– ‘Range’
– ‘Intensity’ to limitate the effect
– ‘Lightmapping’ Realtime Only, non deve rimanere impressa nella texture precalcolata

– Attach the scripts:

LaserBlinking.js

#pragma strict

public var onTime : float;          // Amount of time in seconds the laser is on for.
public var offTime : float;         // Amount of time in seconds the laser is off for.


private var timer : float;          // Timer to time the laser blinking.


function Update ()
{
    // Increment the timer by the amount of time since the last frame.
    timer += Time.deltaTime;
    
    // If the beam is on and the onTime has been reached...
    if(renderer.enabled && timer >= onTime)
        // Switch the beam.
        SwitchBeam();
    
    // If the beam is off and the offTime has been reached...
    if(!renderer.enabled && timer >= offTime)
        // Switch the beam.
        SwitchBeam();
}


function SwitchBeam ()
{
    // Reset the timer.
    timer = 0f;
    
    // Switch whether the beam and light are on or off.
    renderer.enabled = !renderer.enabled;
    light.enabled = !light.enabled;
}

LaserPlayerDetection.js

#pragma strict

private var player : GameObject;                            // Reference to the player.
private var lastPlayerSighting : LastPlayerSighting;        // Reference to the global last sighting of the player.


function Awake ()
{
    // Setting up references.
    player = GameObject.FindGameObjectWithTag(Tags.player);
    lastPlayerSighting = GameObject.FindGameObjectWithTag(Tags.gameController).GetComponent(LastPlayerSighting);
}


function OnTriggerStay(other : Collider)
{
    // If the beam is on...
    if(renderer.enabled)
        // ... and if the colliding gameobject is the player...
        if(other.gameObject == player)
            // ... set the last global sighting of the player to the colliding object's position.
            lastPlayerSighting.position = other.transform.position;
}

Inspector setup
– var OnTime = 1.5
– var OffTime = 1.5

2. Hierarchy> ‘Laser Grid’ GameObject DRAG AND DROP over Project> Prefabs

3. Delete the original ‘Laser Grid’ GameObject and you can start use Prefabs to instantiate.

Switch Unit

1. Add the Mesh of Switch Unit

2. Add Sphere Collider ‘As Trigger’ to detect the player

3. Attach LaserSwitchDeactivation.js

#pragma strict

public var laser : GameObject;              // Reference to the laser that can we turned off at this switch.
public var unlockedMat : Material;          // The screen's material to show the laser has been unloacked.

private var player : GameObject;            // Reference to the player.


function Awake ()
{
    // Setting up the reference.
    player = GameObject.FindGameObjectWithTag(Tags.player);
}


function OnTriggerStay (other : Collider)
{
    // If the colliding gameobject is the player...
    if(other.gameObject == player)
        // ... and the switch button is pressed...
        if(Input.GetButton("Switch"))
            // ... deactivate the laser.
            LaserDeactivation();
}


function LaserDeactivation ()
{
    // Deactivate the laser GameObject.
    laser.SetActive(false);
    
    // Store the renderer component of the screen.
    var screen : Renderer = transform.Find("prop_switchUnit_screen_001").renderer;
    
    // Change the material of the screen to the unlocked material.
    screen.material = unlockedMat;
    
    // Play switch deactivation audio clip.
    audio.Play();
}

Inspector DRAG AND DROP inside:
– var ‘laser’ -> the Laser Grid you want deactivate
– var ‘unlockedMat’ -> render Switch Unit material as unlocked