Unity 3D – Stealth Game – Laser Grids

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

By |Unity3D, Video Games Development|Commenti disabilitati su Unity 3D – Stealth Game – Laser Grids

Unity 3D Games – Video Surveillance – CCTV Camera – JS Script

Unity 3D Games – Video Surveillance – CCTV Camera – JS Script

1. Inserire il modello 3D della camera di sorveglianza nella scena
2. Imparentare come figlio un oggetto con un ‘Mesh Collider’ come ‘Trigger’ a piramide che simula l’area di visione della camera.
3. Gestisce la collisione tra il ‘Playe’r e il ‘Trigger’ assegnado al ‘Mesh Collider’:


#pragma strict

private var player : GameObject;                                // Reference to the player. Assegnare in Inspector l'oggetto da rilevare
private var lastPlayerSighting : LastPlayerSighting;            // Reference to the global last sighting of the player.


function Awake ()
{
    // Setting up the references.
    // Rileva l'oggetto Player in base al tag -player-
    player = GameObject.FindGameObjectWithTag(Tags.player); 
    // Rileva l'oggetto Game Controller in base al tag -gameControllerr- e ottiene lo script LastPlayerSighting
    lastPlayerSighting = GameObject.FindGameObjectWithTag(Tags.gameController).GetComponent(LastPlayerSighting);
}


function OnTriggerStay (other : Collider)
{
    // If the colliding gameobject is the player...
    // Se collide l'oggetto player...
    if(other.gameObject == player)
    {
        // ... raycast from the camera towards the player.
        // Emetti Raycast, in questo modo il plyaer viene rilevato solo se si trova di fronte alla camera
        // CONSIDERAZIONE: come soluzione semplificata avrei creato un piccolo 'Mesh Collider' a forma di cubo per simulare il RayCast,
        // avrei risolto tutto con una sola collisione, risparmiando il calcolo Raycasting!
        var relPlayerPos : Vector3 = player.transform.position - transform.position;
        var hit : RaycastHit;
        
        if(Physics.Raycast(transform.position, relPlayerPos, hit))
            // If the raycast hits the player...
            // se il Raycast colpisce il player
            if(hit.collider.gameObject == player)
                // ... set the last global sighting of the player to the player's position.
                // fai partire l'allarme
                lastPlayerSighting.position = player.transform.position;
    }
}

CONSIDERAZIONE E OTTIMIZZAZIONE: come soluzione semplificata avrei creato un piccolo ‘Mesh Collider’ a forma di cubo per simulare il RayCast, avrei risolto tutto con una sola collisione, risparmiando il calcolo Raycasting!

By |Unity3D, Video Games Development|Commenti disabilitati su Unity 3D Games – Video Surveillance – CCTV Camera – JS Script

Unity 3D – JS Script – gameController

Unity 3D – JS Script – gameController

BG Music multiple tracks management

1. MAIN TOP MENU> GameObject> Create Empty, name it ‘gameController’
2. ‘Add Component’> Audio> Audio Source>
– Audio Clip, DRAG AND DROP the background music
– Play On Awake: check
– Loop: check

3. 1. MAIN TOP MENU> GameObject> Create Empty, name it ‘secondaryMusic’
4. ‘Add Component’> Audio> Audio Source>
– Audio Clip, DRAG AND DROP the secondary music
– Play On Awake: check
– Loop: check
– Volume = 0 if the volume will be >0 you listen 2 audio source at the same time!

5. Hierarchy DRAG AND DROP ‘secondaryMusic’ over ‘gameController’, now:
‘gameController'(parent)
|->’secondaryMusic’ (child)

Sighting (avvistamento)

Hierarchy> env_stealth_static> props> select CTRL + All prop_megaphone_00etc…> Inspector> Tag ‘Siren’
Open the 3D megaphone game objects and assign Tag ‘Siren’.
Se ci sarà un avvistamento tutti i megafoni 3D dovranno suonare contemporaneamente.

Con ancora tutti gli elementi selezionati assegnare il suono dell’allarme con
Inspector> Add Component> Audio> Audio Source> Audio Clip> DRAG AND DROP from Project> Audio> alarm_triggered
– Play On Awake: uncheck
– Loop: check
– 3D Sound Settings> Min Distance> 5, l’audio si sentirà se si entra nel raggio di 5 unità dall’oggetto 3D, l’area delimitata nella viewport dalla sfera azzurra.

6. Hierarchy ‘gameController’> Inspector> ‘Add Component’> LastPlayerSighting.js

#pragma strict

public var position : Vector3 = new Vector3(1000f, 1000f, 1000f);       // The last global sighting of the player.
public var resetPosition : Vector3 = new Vector3(1000f, 1000f, 1000f);  // The default position if the player is not in sight.
public var lightHighIntensity : float = 0.25f;                          // The directional light's intensity when the alarms are off.
public var lightLowIntensity : float = 0f;                              // The directional light's intensity when the alarms are on.
public var fadeSpeed : float = 7f;                                      // How fast the light fades between low and high intensity.
public var musicFadeSpeed : float = 1f;                                 // The speed at which the 


private var alarm : AlarmLight;                                         // Reference to the AlarmLight script.
private var mainLight : Light;                                          // Reference to the main light.
private var panicAudio : AudioSource;                                   // Reference to the AudioSource of the panic msuic.
private var sirens : AudioSource[];                                     // Reference to the AudioSources of the megaphones.


function Awake ()
{
    // Setup the reference to the alarm light.
    alarm = GameObject.FindGameObjectWithTag(Tags.alarm).GetComponent(AlarmLight);
    
    // Setup the reference to the main directional light in the scene.
    mainLight = GameObject.FindGameObjectWithTag(Tags.mainLight).light;
    
    // Setup the reference to the additonal audio source.
    panicAudio = transform.Find("secondaryMusic").audio;
    
    // Find an array of the siren gameobjects.
    var sirenGameObjects : GameObject[] = GameObject.FindGameObjectsWithTag(Tags.siren);
    
    // Set the sirens array to have the same number of elements as there are gameobjects.
    sirens = new AudioSource[sirenGameObjects.Length];
    
    // For all the sirens allocate the audio source of the gameobjects.
    for(var i = 0; i < sirens.Length; i++)
    {
        sirens[i] = sirenGameObjects[i].audio;
    }
}


function Update ()
{
    // Switch the alarms and fade the music.
    SwitchAlarms();
    MusicFading();
}


function SwitchAlarms ()
{
    // Set the alarm light to be on or off.
    alarm.alarmOn = position != resetPosition;
    
    // Create a new intensity.
    var newIntensity : float;
    
    // If the position is not the reset position...
    if(position != resetPosition)
        // ... then set the new intensity to low.
        newIntensity = lightLowIntensity;
    else
        // Otherwise set the new intensity to high.
        newIntensity = lightHighIntensity;
    
    // Fade the directional light's intensity in or out.
    mainLight.intensity = Mathf.Lerp(mainLight.intensity, newIntensity, fadeSpeed * Time.deltaTime);
    
    // For all of the sirens...
    for(var i = 0; i < sirens.Length; i++)
    {
        // ... if alarm is triggered and the audio isn't playing, then play the audio.
        if(position != resetPosition && !sirens[i].isPlaying)
            sirens[i].Play();
        // Otherwise if the alarm isn't triggered, stop the audio.
        else if(position == resetPosition)
            sirens[i].Stop();
    }
}


function MusicFading ()
{
    // If the alarm is not being triggered...
    if(position != resetPosition)
    {
        // ... fade out the normal music...
        audio.volume = Mathf.Lerp(audio.volume, 0f, musicFadeSpeed * Time.deltaTime);
        
        // ... and fade in the panic music.
        panicAudio.volume = Mathf.Lerp(panicAudio.volume, 0.8f, musicFadeSpeed * Time.deltaTime);
    }
    else
    {
        // Otherwise fade in the normal music and fade out the panic music.
        audio.volume = Mathf.Lerp(audio.volume, 0.8f, musicFadeSpeed * Time.deltaTime);
        panicAudio.volume = Mathf.Lerp(panicAudio.volume, 0f, musicFadeSpeed * Time.deltaTime);
    }
}
By |Unity3D, Video Games Development|Commenti disabilitati su Unity 3D – JS Script – gameController

Unity 3D – JS Script – Screen Fader

Unity 3D – JS Script – Screen Fader

1. MAIN TOP MENU> GameObject> Create Other> GUI TEXTURE> Inspector, rename it ‘ScreenFader’
2. Inspector> Transform> Position X=0 Y=0 Z=0 (così viene posizionata in basso a sinistra dello schermo)
3. Pixel Inset, X=0 Y=0 W=0 H=0 (non vogliamo che ci siano dei rientri)
4. Inspector> GUI Texture> DRAG E DROP a black bitmap Texture
5. Assign to ‘ScreenFader’ GameObject this JS Script:

SceneFadeInOut.js

#pragma strict

public var fadeSpeed : float = 1.5f;            // Speed that the screen fades to and from black.
private var sceneStarting : boolean = true;     // Whether or not the scene is still fading in.

function Awake ()
{
    // Set the texture so that it is the size of the screen and covers it.
    // Per prima cosa ricopro tutto lo schermo creando un rettangolo di dimensione dello schermo
    guiTexture.pixelInset = new Rect(0f, 0f, Screen.width, Screen.height);
}


function Update ()
{
    // If the scene is starting...
    // Controlla il valore booleano -sceneStarting- se è vero esegue le istruzioni all'interno di -if-
    if(sceneStarting)
        // ... call the StartScene function.
        // richiama la funzione
        StartScene();
}


function FadeToClear ()
{
    // Lerp (interpolate 2 values) the colour of the texture between itself and transparent.
    // Esegue un fading - colore della texture -> completamente trasparente -
    guiTexture.color = Color.Lerp(guiTexture.color, Color.clear, fadeSpeed * Time.deltaTime);
}


function FadeToBlack ()
{
    // Lerp (interpolate 2 values) the colour of the texture between itself and black.
    guiTexture.color = Color.Lerp(guiTexture.color, Color.black, fadeSpeed * Time.deltaTime);
}


function StartScene ()
{
    // Fade the texture to clear.
    FadeToClear();
    
    // If the texture is almost clear...
    // Se il colore è quasi completamente trasparente
    if(guiTexture.color.a <= 0.05f)
    {
        // ... set the colour to clear and disable the GUITexture.
        // Rendi il colore completamente trasparente e disabilita la GUI Texture
        guiTexture.color = Color.clear;
        guiTexture.enabled = false;
        
        // The scene is no longer starting.
        // Imposta la variabile booleana a falso per interrompere il ciclo
        sceneStarting = false;
    }
}


public function EndScene ()
{
    // Make sure the texture is enabled.
    guiTexture.enabled = true;
    
    // Start fading towards black.
    FadeToBlack();
    
    // If the screen is almost black...
    if(guiTexture.color.a >= 0.95f)
        // ... reload the level.
        Application.LoadLevel(0);
}

Inspector> SceneFadeInOut.js> ‘Fade Speed’ variable, assign 0.5 (valore basso -> tempo di fading alto)

NOTICE:

Application.LoadLevel(0);

Setup the correct level number, you can find it inside MAIN TOP MENu> File> Build Settings…
If you have only one scene in your project, just reload it -> .LoadLevel(0)

By |Unity3D, Video Games Development|Commenti disabilitati su Unity 3D – JS Script – Screen Fader

Unity – Advanced Tags Management

In order to minimize errors we can put our Tags inside a script.
Oper the Project> Script Folder> Create New Js Script

#pragma strict

// A list of tag strings.
public static var player : String = "Player";
public static var alarm : String = "AlarmLight";
public static var siren : String = "Siren";
public static var gameController : String = "GameController";
public static var mainLight : String = "MainLight";
public static var fader : String = "Fader";
public static var enemy : String = "Enemy";
By |Unity3D, Video Games Development|Commenti disabilitati su Unity – Advanced Tags Management