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

Unity 3D Game Engine – Android – Touch Drag – Moving a 3D Object – JS

Unity 3D Game Engine – Android – Touch Drag – Moving a 3D Object – JS

Inside Hierarchy create:

– Cube (Game Object), position X=0 Y=0.5 Z=0

– Main Camera, move it to see the Cube from top; position X=0 Y=8 Z=0, rotation X=90 Y=0 Z=0, attach the script ‘TouchMove.js’

TouchMove.js


var object : GameObject;

function Update () {
   
	for (var touch : Touch in Input.touches){
		var ray = Camera.main.ScreenPointToRay(touch.position);
		var hit : RaycastHit;
		if (Physics.Raycast (ray, hit, 100)) {
			if(touch.phase == TouchPhase.Began || touch.phase == TouchPhase.Moved) {
				var cameraTransform = Camera.main.transform.InverseTransformPoint(0, 0, 0);
				object.transform.position = Camera.main.ScreenToWorldPoint(new Vector3 (touch.position.x, touch.position.y, cameraTransform.z - 0.5));
			}
		}
	}
}

Hieararchy> Main Camera> Inspector> TouchMove.js> DRAG AND DROP Cube (Game Object) over public var object

By |Unity3D, Video Games Development|Commenti disabilitati su Unity 3D Game Engine – Android – Touch Drag – Moving a 3D Object – JS

Unity 3D Game Engine – Android – TouchPhase – Screen – Began-Moved-Stationary-Ended-Canceled

Unity 3D Game Engine – Android – TouchPhase – Screen – Began-Moved-Stationary-Ended-Canceled

This Script check the Touch Phase on entire Screen.
– TouchPhase.Began – A finger touched the screen
– TouchPhase.Moved – A finger moved on the screen
– TouchPhase.Stationary – A finger is touching the screen but hasn’t moved
– TouchPhase.Ended – A finger was lifted from the screen. This is the final phase of a touch
– TouchPhase.Canceled – The system cancelled tracking for the touch

1. Inside Hierarchy create the structure:

– GUI Text (GameObject)
– Main Camera, attach the script ‘TouchPhaseCheck.js’

TouchPhaseCheck.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;

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

function Update () {

	// TouchPhase.Began - A finger touched the screen
	// 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) {
	scoreText.text = "TouchPhase.Began";
	}
	
	// TouchPhase.Moved - A finger moved on the screen
	if (Input.touchCount > 0 && Input.GetTouch(0).phase == TouchPhase.Moved) {
	scoreText.text = "TouchPhase.Moved";
	}
	
	// TouchPhase.Stationary - A finger is touching the screen but hasn't moved
	if (Input.touchCount > 0 && Input.GetTouch(0).phase == TouchPhase.Stationary) {
	scoreText.text = "TouchPhase.Stationary";
	}
	
	// TouchPhase.Ended - A finger was lifted from the screen. This is the final phase of a touch
	if (Input.touchCount > 0 && Input.GetTouch(0).phase == TouchPhase.Ended) {
	scoreText.text = "TouchPhase.Ended";
	}
	
	// TouchPhase.Canceled - The system cancelled tracking for the touch
	// This might happen if, for example, the user puts the device to her face 
        // or simultaneously applies more touches than the system can track (the exact number varies with different platforms) 
	// This is the final phase of a touch.
	if (Input.touchCount > 0 && Input.GetTouch(0).phase == TouchPhase.Canceled) {
	scoreText.text = "TouchPhase.Canceled";
	}
}

2. Hierarchy> Main Camera> Inspector> ‘TouchPhaseCheck.js’ DRAG AND DROP GUI Text (GameObject) over var scoreText

By |Unity3D, Video Games Development|Commenti disabilitati su Unity 3D Game Engine – Android – TouchPhase – Screen – Began-Moved-Stationary-Ended-Canceled

Unity 3D Game Engine – Camera – 2D Platform – Smooth Follow

Unity 3D Game Engine – Camera – 2D Platform – Smooth Follow

1. Inside Hierarchy create the structure:

– Player (parent)
— CameraTarget (Empty Object) (child)

– Main Camera

2. Select ‘Main Camera’ and create ‘CameraController.js’

CameraController.js


#pragma strict
 
var cameraTarget : GameObject; // Inspector> Assign the Camera Target NON è il target della camera ma la posizione che vuole raggiungere la camera

var smoothTime : float = 0.1;              // Delay in seconds to follow Player
var cameraFollowX : boolean = true;        // Inspector> if is checked -> The Camera will follow X position of cameraTarget
var cameraFollowY : boolean = true;        // Inspector> if is checked -> The Camera will follow Y position of cameraTarget
var cameraFollowHeight : boolean = false;  // if true the Camera Y Position = cameraHeight
var cameraHeight : float = 2.5;            // cameraHeight
var velocity : Vector2;
private var thisTransform : Transform;    

function Start ()
{
  thisTransform = transform;
}

function Update () 
{

if (cameraFollowX) // if cameraFollowX = true = Inspector is checked
{
  thisTransform.position.x = Mathf.SmoothDamp (thisTransform.position.x, cameraTarget.transform.position.x, velocity.x, smoothTime);
}

if (cameraFollowY) // if cameraFollowY = true = Inspector is checked
{
  thisTransform.position.y = Mathf.SmoothDamp (thisTransform.position.y, cameraTarget.transform.position.y, velocity.y, smoothTime);
}

if (!cameraFollowY && cameraFollowHeight)     // if cameraFollowY = false = Inspector is unchecked AND cameraFollowHeight = true = Inspector is checked
{
  camera.transform.position.y = cameraHeight; // The Camera Y position = cameraHeight
}

}

Main Camera> Inspector> LookAt.js> DRAG AND DROP over:
– var Camera Target -> CameraTarget (Empty Object)
– var Player -> Player (Game Object)
– var Smoot Time -> Delay time to reach final position

a. Camera Follow X check | Camera Follow Y check | Camera Follow Height uncheck | Camera Height
-> follow X and Y of cameraTarget

b. Camera Follow X uncheck | Camera Follow Y check | Camera Follow Height uncheck | Camera Height
-> follow Y of cameraTargetr the X value is the current X position in the viewport

c. Camera Follow X check | Camera Follow Y uncheck | Camera Follow Height check | Camera Height
-> follow X of cameraTarget and Y Camera Height value

By |Unity3D, Video Games Development|Commenti disabilitati su Unity 3D Game Engine – Camera – 2D Platform – Smooth Follow