code snippets

Unity 3D Game Engine – Android – Multi Touch – Pich to Zoom – JavaScript

Unity 3D Game Engine – Android – Multi Touch – Pich to Zoom – JavaScript

ZommIn ZoomOut the camera using pinch gesture.

Inside Hierarchy create:

1. Cube (Gameobject)

2. Main Camera, attach the script ‘PinchZoom.js’

PinchZoom.js:


#pragma strict

// Pinch to Zoom
// Attach this script to Main Camera

public var perspectiveZoomSpeed : float = 0.5f;  // The rate of change of the field of view in perspective mode. E' la velocità con la quale si avrà lo zoom
public var orthoZoomSpeed : float = 0.5f;        // The rate of change of the orthographic size in orthographic mode.  E' la velocità con la quale si avrà lo zoom


function Update()
{
    // If there are two touches on the device...
    if (Input.touchCount == 2)
    {
        // Store both touches.
        var touchZero = Input.GetTouch(0);
        var touchOne = Input.GetTouch(1);

        // Find the position in the previous frame of each touch.
        var touchZeroPrevPos = touchZero.position - touchZero.deltaPosition;
        var touchOnePrevPos = touchOne.position - touchOne.deltaPosition;

        // Find the magnitude of the vector (the distance) between the touches in each frame.
        var prevTouchDeltaMag = (touchZeroPrevPos - touchOnePrevPos).magnitude;
        var touchDeltaMag = (touchZero.position - touchOne.position).magnitude;

        // Find the difference in the distances between each frame.
        var deltaMagnitudeDiff = prevTouchDeltaMag - touchDeltaMag;

        // If the camera is orthographic...
        if (camera.isOrthoGraphic)
        {
            // ... change the orthographic size based on the change in distance between the touches.
            camera.orthographicSize += deltaMagnitudeDiff * orthoZoomSpeed;

            // Make sure the orthographic size never drops below zero.
            camera.orthographicSize = Mathf.Max(camera.orthographicSize, 0.1f);
        }
        else
        {
            // Otherwise change the field of view based on the change in distance between the touches.
            camera.fieldOfView += deltaMagnitudeDiff * perspectiveZoomSpeed;

            // Clamp the field of view to make sure it's between 0 and 180.
            // Clam in inglese significa 'morsetto', significa limitare il valore ad un valore massimo ed un valore minimo prestabilito
            camera.fieldOfView = Mathf.Clamp(camera.fieldOfView, 0.1f, 179.9f);
        }
    }
}

By |Unity3D, Video Games Development|Commenti disabilitati su Unity 3D Game Engine – Android – Multi Touch – Pich to Zoom – JavaScript

Unity 3D Game Engine – Android – Tap Single – JS

Unity 3D Game Engine – Android – Tap Single – JS

Simple Android application, if you touch the screen it will change the GUI Text content, No Touch -> Touched!

1. Inside Hierarchy create the structure:

– Main Camera
– GUI Text
– GameController (Empty Object) attach the ‘TouchController.js’

TouchController.js


#pragma strict

// Hierarchy DRAG E DROP over var GUI Text in Inspector
var scoreText : GUIText;

function Start () {
        scoreText.text = "No Touch";
}

function Update() {
    if (Input.touchCount == 1) {
        // Do something
        scoreText.text = "Touched!";
    }
}

2. Hierarchy> GameController> TouchController.js> DRAG AND DROP ‘GUI Text’ Object over var ‘scoreText’

By |Unity3D, Video Games Development|Commenti disabilitati su Unity 3D Game Engine – Android – Tap Single – JS

Unity 3D Game Engine – Android – Tap Single – Destroy Object – JS

Unity 3D Game Engine – Android – Tap Single – Destroy Object – JS

With this JS Script you will destroy objects if you tap them.

Inside Hierarchy create the structure:

– Ball (Game Object)
– Main Camera, attach the script ‘TouchDestroy.js’

TouchDestroy.js


#pragma strict

    // ONLY ANDROID NOT PC NOT IOS
    // Attach this script to the Main Camera
    // It raycasts from camera and destroies objects if you will touch them

var speed : float = 4;
var hit = new RaycastHit();

function Update () {

	// se c'è un tocco Input.touchCount AND la fase del tocco è quella iniziale TouchPhase.Began
	if (Input.touchCount > 0 && Input.GetTouch(0).phase == TouchPhase.Began) {
	// traccia i raggi dalla Camera dal punto del tocco
	var ray = Camera.main.ScreenPointToRay (Input.GetTouch(0).position);

		// se raycast colpisce l'oggetto
		if (Physics.Raycast (ray, hit)) {
		// distruggi l'oggetto colpito
		Destroy(hit.transform.gameObject);
		}

	}
}

By |Unity3D, Video Games Development|Commenti disabilitati su Unity 3D Game Engine – Android – Tap Single – Destroy Object – JS

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

CSS – Tooltip Hovers

CSS – Tooltip Hovers

By |CSS, Web Design|Commenti disabilitati su CSS – Tooltip Hovers