Video Games Development

Unity 3D Game Engine – Android – Tap Single – Counter Increase – JavaScript

Unity 3D Game Engine – Android – Tap Single – Counter Increase – JavaScript

This script it is a simple Tap Counter, if you Tap over the objects in the scene counter value will increase.

Inside Hierarchy create the game Objects:

– Sphere (GameObject)
– GUI Text (GameObject)
– Main Camera, attach the ‘TapCounter.js’:

TapCounter.js


#pragma strict

    // Only ANDROID NOT PC NOT IOS
    // Attach this script to the Main Camera
    
// 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;

// Ray Cast Setup
var speed : float = 4;
var hit = new RaycastHit();

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

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)) {
		// fai partire la funzione che incrementa il contatore
		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> Main Camera> Inspector> TapCounter.js DRAG AND DROP ‘GUI Text’ (GameObject) over var scoreText

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

Unity 3D Game Engine – Android – Tap Single – New Object – Instantiate – JS

Unity 3D Game Engine – Android – Tap Single – New Object – Instantiate – JS

Instantiate new objects by tapping on Android Devices (Unity JavaScript).

1. Hierarchy create the structure:

– Cube (Game Object)
– Main Camera X=0 Y=0 Z=-10, attach the ‘TapInstantiate.js’

TapInstantiate.js


// Main Camera> Inspector> assign a Gameobject of Prefab
var bullet : Transform;

function Update () {  
    
		// Se c'è un tocco  AND  la fase è il primo contatto del dito con il display
		if (Input.touchCount > 0 && Input.GetTouch(0).phase == TouchPhase.Began) {
		// Your 2D vector
		var touchPos = Input.GetTouch(0).position;

		// New 3D vector with the Z co-ordinate
		// As your camera's Z is at -10, setting it to 10 will create it at 0 in the world
		// Change z value to instantiate the object near or far the Camera 
		var createPos = Camera.main.ScreenToWorldPoint(Vector3(touchPos.x, touchPos.y, 10));

		Instantiate(bullet, createPos, Quaternion.identity);
        }
}

2. Hierarchy> Main Camera> Inspector> TapInstantiate.js> DRAG AND DROP Cube over public var bullet

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

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 Game Engine – Android – Tap Single – Specific Object (Tagged) – Counter Increase

Unity 3D Game Engine – Android – Tap Single – Specific Object (Tagged) – Counter Increase

Simple JavaScript for Android.
Single Tap a specific object to increase a counter.

Inside Hierarchy create the structure:

– Sphere (Gameobject) -> Inspector> Assign Tag ‘Button1’
– Cube (Gameobject)
– GUI Text (Gameobject)
– Main Camera, attach the script ‘TapControl.js’

TapControl.js


#pragma strict

    // Only ANDROID NOT PC NOT IOS
    // Attach this script to the Main Camera
    
// 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;

// Ray Cast Setup
var speed : float = 4;
var hit = new RaycastHit();

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

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)) {
		
		 	// se collidi con un oggetto con tag Button1
		 	if (hit.collider.tag == "Button1"){ 
                        // fai partire la funzione che incrementa il contatore
		        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> Main Camera> Inspector> TapControl.js> DRAG AND DROP ‘GUI Text’ (Gameobject) over var scoreText

By |Unity3D, Video Games Development|Commenti disabilitati su Unity 3D Game Engine – Android – Tap Single – Specific Object (Tagged) – Counter Increase