unity3d

Unity 3D – Stealth Game – HashIDs

Unity 3D – Stealth Game – HashIDs

Here we store the hash tags for various strings used in our animators.

1. Hierarchy> ‘gameController’


#pragma strict

// Here we store the hash tags for various strings used in our animators.
public var dyingState : int;
public var locomotionState : int;
public var shoutState : int;
public var deadBool : int;
public var speedFloat : int;
public var sneakingBool : int;
public var shoutingBool : int;
public var playerInSightBool : int;
public var shotFloat : int;
public var aimWeightFloat : int;
public var angularSpeedFloat : int;
public var openBool : int;


function Awake ()
{
    dyingState = Animator.StringToHash("Base Layer.Dying");
    locomotionState = Animator.StringToHash("Base Layer.Locomotion");
    shoutState = Animator.StringToHash("Shouting.Shout");
    deadBool = Animator.StringToHash("Dead");
    speedFloat = Animator.StringToHash("Speed");
    sneakingBool = Animator.StringToHash("Sneaking");
    shoutingBool = Animator.StringToHash("Shouting");
    playerInSightBool = Animator.StringToHash("PlayerInSight");
    shotFloat = Animator.StringToHash("Shot");
    aimWeightFloat = Animator.StringToHash("AimWeight");
    angularSpeedFloat = Animator.StringToHash("AngularSpeed");
    openBool = Animator.StringToHash("Open");
}

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

Unity 3D – Stealth Game – Player Animator Controller

Unity 3D – Stealth Game – Player Animator Controller

1. Projects> Animator folder> Create> Animator Controller> name it ‘PlayerAnimator’

2. MAIN TOP MENU> Window> Animator

3. Animator> BOTTOM LEFT> Parameters

– Speed -> float (velocità)
– Sneaking -> bool (spiare)
– Dead -> bool (morte)
– Shouting -> bool (urlare)

4. Project> Animations> Humanoid> humanoid_idle (inattivo) (box blu)> Idle (animazione) DRAG AND DROP inside Animator window

5. Hierarchy> char_ethan> Inspector> Animator> Controller DRAG ‘PlayerAnimator’ was created at 1.

6. Play

Idle -> Walk -> Run | Run -> Walk -> Idle

7. Animator> RMB> Create State> From New Blend Tree> Inspector> rename it ‘Locomotion’

8. Animator> LMB Double Click> Blend Tree (notare che il layer attuale è Base Layer> Locomotion)

> Inspector, rename it ‘Locomotion’

> Inspector> + Add Motion Field> little circle icon> Walk

> Inspector> + Add Motion Field> little circle icon> Run

> Inspector> Automate Threshold uncheck (soglia automatica)

> Inspector> Compute Threshold> Speed

9. Animator>

> Base Layer (torno al livello base)> ‘Idle’ RMB> Make Transition> DRAG over ‘Locomotion’

> LMB over the Transition Line to select it> Inspector>

>Conditions> Speed> Greater> 0.1

>Conditions> +> Sneaking> false

10. Animator>

> Base Layer (torno al livello base)> ‘Locomotion’ RMB> Make Transition> DRAG over ‘Idle’

> LMB over the Transition Line to select it> Inspector>

>Conditions> Speed> Less> 0.1

Any State -> Die

Create a Transition from ‘Any State’ -> ‘Dying’

Conditions> Dead> true

NOTA BENE:
‘Any State’ significa che si può arrivare alla morte da qualsiasi stato, senza dover creare transizioni complicate dagli altri stati.

Avatar Mask

The Avatar Mask is an additional layer of animation.

1. Project> Animations folder> Create> Avatar Mask, name it ‘PlayerShoutingMask’> Inspector> Humanoid> deselect (they will turn red) the body parts you do not want modify with the Avatar Mask.

By |Unity3D, Video Games Development|Commenti disabilitati su Unity 3D – Stealth Game – Player Animator Controller

Unity 3D – Stealth Game – Player Setup

Unity 3D – Stealth Game – Player Setup

1. DRAG the model into the scene from Projects to Hierarchy

2. ‘Add Component’> Physic> Capsule Collider

3. ‘Add Component’> Audio> Audio Source> DRAG player_footsteps.wav
– ‘Play on Awake’ uncheck
– ‘Loop’ check
– ‘Volumes’ = 0.1 We don’t want footsteps to be too intrusive

– Remove the Audio Listener of camera_main
– Select the player> ‘Add Component’> Audio> Audio Listener

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

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