unity3d

Unity 3D Game Engine – Android – Accelerometer – Shake – JavaScript

Unity 3D Game Engine – Android – Accelerometer – Shake – JavaScript

Hierarchy:

– Main camera
– GUI Text
– Cube, attach the script ‘CheckShake.js’

CheckShake.js


#pragma strict

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

var accelerometerUpdateInterval : float = 1.0 / 60.0;

// The greater the value of LowPassKernelWidthInSeconds, the slower the filtered value will converge towards current input sample (and vice versa).
var lowPassKernelWidthInSeconds : float = 1.0;

// This next parameter is initialized to 2.0 per Apple's recommendation, or at least according to Brady! 😉
var shakeDetectionThreshold : float = 2.0;

private var lowPassFilterFactor : float = accelerometerUpdateInterval / lowPassKernelWidthInSeconds; 
private var lowPassValue : Vector3 = Vector3.zero;
private var acceleration : Vector3;
private var deltaAcceleration : Vector3;


function Start()

{
shakeDetectionThreshold *= shakeDetectionThreshold;
lowPassValue = Input.acceleration;
}


function Update()
{

acceleration = Input.acceleration;
lowPassValue = Vector3.Lerp(lowPassValue, acceleration, lowPassFilterFactor);
deltaAcceleration = acceleration - lowPassValue;
    if (deltaAcceleration.sqrMagnitude >= shakeDetectionThreshold)
    {
        // Perform your "shaking actions" here, with suitable guards in the if check above, if necessary to not, to not fire again if they're already being performed.
        scoreTextX.text = "Shake event detected at time "+Time.time;
    }

}

Hierarchy> Cube> Inspector> CheckShake.js> DRAG AND DROP ‘GUI Text’ GameObject over public var scoreTextX

Reference:
http://forum.unity3d.com/threads/15029-Iphone-Shaking?p=206342#post206342

By |Unity3D, Video Games Development|Commenti disabilitati su Unity 3D Game Engine – Android – Accelerometer – Shake – JavaScript

Unity 3D Game Engine – Long Touch – Counter Increase Decrease

Unity 3D Game Engine – Long Touch – Counter Increase Decrease

The counter will increase if you tap or long touch the device
The counter will decrease if you do not touch the device

1. Inside the 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;
// touch counter, private because the users is not be able to change this value
private  var score : int;

function Start () {
        // The counter initial value is 0
        score = 0;
        scoreText.text = "No Touch:";
}

function Update() {

    // upper and lower limit for score
    LimitScore ();

    if (Input.touchCount == 1) {
        // var score will increase if you tap or long touch the device
        UpdateScore ();
    }
    
    // every frame score will decrease
    DecreaseScore ();
}

function UpdateScore () {
    // score var increment of +10
    // NOTICE!!!! UpdateScore() DEVE essere più veloce di DecreaseScore() altrimenti non si incrementa,
    // infatti si basano sulla stessa velocità di esecuzione
    // per rallentare DecreaseScore() si potrebbe creare un ritardo di esecuzione - yield WaitForSeconds (5);
    score += 10;
    // scoreText in assigned inside Inspector on GUI Text
    // change .text property and write it on the display 
    scoreText.text = "Touched: "  + score;
}

function DecreaseScore () {
    // per rallentare DecreaseScore() si potrebbe creare un ritardo di esecuzione - yield WaitForSeconds (5);
    // score var decrement of -1
    score -= 1;
    // scoreText in assigned inside Inspector on GUI Text
    // change .text property and write it on the display 
    scoreText.text = "Touched: "  + score;
}

function LimitScore () {
    if(score < 1)    // minimum score value
    	score = 1; 
    if(score > 1000) // maximum score value, at the end it can reach 999+10=1009
    	score = 1000;
    scoreText.text = "Touched: "  + score;
}

Hierarchy> GameController> TouchController.js assign over var Score Text the GUI Text Object

By |Unity3D, Video Games Development|Commenti disabilitati su Unity 3D Game Engine – Long Touch – Counter Increase Decrease

Unity 3D Game Engine – Long Touch – Counter Increase

Unity 3D Game Engine – Long Touch – Counter Increase

The counter will increase if you tap or long touch the device

1. Inside the 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;
// touch counter, private because the users is not be able to change this value
private  var score : int;

function Start () {
        // The counter initial value is 0
        score = 0;
        scoreText.text = "No Touch:";
}

function Update() {
    if (Input.touchCount == 1) {
        // var score will increase if you tap or long touch the device
        UpdateScore ();
    }
}

function UpdateScore () {
    // score var increment of +1
    score += 1;
    // scoreText in assigned inside Inspector on GUI Text
    // change .text property and write it on the display 
    scoreText.text = "Touched: "  + score;
}


Hierarchy> GameController> TouchController.js assign over var Score Text the GUI Text Object

By |Unity3D, Video Games Development|Commenti disabilitati su Unity 3D Game Engine – Long Touch – Counter Increase

Unity 3D Game Engine – Android – Multi Touch – Basics

Unity 3D Game Engine – Android – Multi Touch – Basics

With this JavaScript code you can check multi touch over an Android device.

1. Create a GUI Text
2. Main Camera, attach the script ‘MultiTouchDetector.js’

MultiTouchDetector.js


#pragma strict

// Rileva il numero di tocchi che avviene contemporaneamente sul dispositivo

var TouchesCounter  : GUIText;  // to display position of finger, Hierarchy DRAG E DROP over var GUI Text in Inspector

function Update () {  
    
	// If there are 1 touches on the device...
    if (Input.touchCount == 1)   
    {
    TouchesCounter.text = "Touches: 1";
	}
    
	
	// If there are 2 touches on the device...
    if (Input.touchCount == 2)  
    {
     TouchesCounter.text = "Touches: 2";
	}
     

	// If there are 3 touches on the device...
    if (Input.touchCount == 3)
    {
    TouchesCounter.text = "Touches: 3";
	}
    
    
    	// If there are 4 touches on the device...
    if (Input.touchCount == 4)  
    {
    TouchesCounter.text = "Touches: 4";
	}
    
    
    	// If there are 5 touches on the device...
    if (Input.touchCount == 5)
    {
    TouchesCounter.text = "Touches: 5";
	}
    
}

Hierarchy> Main Camera> Inspector> MultiTouchDetector.js> DRAG GUI Text over public var TouchesCounter

In this example you can check if over the display you will have 1,2,3,4 or 5 fingers.

By |Unity3D, Video Games Development|Commenti disabilitati su Unity 3D Game Engine – Android – Multi Touch – Basics

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