videogames development

Unity 3D Game Engine – Long Touch – Draw Line – JavaScript

Unity 3D Game Engine – Long Touch – Draw Line – JavaScript

1. Main Camera attach ‘FingerLine.js’

FingerLine.js:


#pragma strict

// It automatically adds Add Component> Effects> Line Renderer
@script RequireComponent(LineRenderer)

var lineRenderer : LineRenderer;
var myPoints : Vector3[];

function Start () {
    // Setup of LineRenderer Component
    lineRenderer = GetComponent(LineRenderer);
    lineRenderer.SetWidth(0.2,0.2);
}

function Update () {

    if(myPoints){
        lineRenderer.SetVertexCount(myPoints.Length);
        for(var i = 0;i<myPoints.Length;i++){
            lineRenderer.SetPosition(i,myPoints[i]);    
        }
    }
    else
    lineRenderer.SetVertexCount(0);
    
    if(Input.touchCount > 0){
    if(Input.touches[0].phase == TouchPhase.Began)
        // it is only drawing 10 points per second, as per the line
        InvokeRepeating("AddPoint",.1,.1);
    } 
    else{
        CancelInvoke();
        myPoints = null;
    }
}

function AddPoint(){
   
    var tempPoints : Vector3[];

    if(!myPoints)
        tempPoints = new Vector3[1];
    else{
        tempPoints = new Vector3[myPoints.Length+1];
               
    	for(var j = 0; j < myPoints.Length; j++)
        	tempPoints[j] = myPoints[j];
    }
        var tempPos : Vector3 = Input.mousePosition;
    tempPos.z = 10;
    
   tempPoints[j] = Camera.main.ScreenToWorldPoint(tempPos);
   myPoints = new Vector3[tempPoints.Length]; 
   myPoints = tempPoints;   
}

NOTICE: it seems to be a little sluggish.

By |Unity3D, Video Games Development|Commenti disabilitati su Unity 3D Game Engine – Long Touch – Draw Line – JavaScript

Unity 3D Game Engine – Android – Swipe – Screen – Move Object

Unity 3D Game Engine – Android – Swipe – Screen – Move Object

Translate a GameObject swiping screen

Hierarchy, create a scene with:

– Main Camera
– Cube (Game Object), attach the script ‘TouchController.js’

TouchController.js


#pragma strict

// Moves object according to finger movement on the screen
	var speed : float = 3.0;
	function Update () {
	    // Se si sta toccando lo schermo e la fase è Moved (il dito sta strisciano)
		if (Input.touchCount > 0 && Input.GetTouch(0).phase == TouchPhase.Moved) {
		
			// Get movement of the finger since last frame
			var touchDeltaPosition:Vector2 = Input.GetTouch(0).deltaPosition;
			
			// Move object across XY plane
			// Time.deltaTime it move xxx meters per second instead of xxx meters per frame
			transform.Translate (touchDeltaPosition.x * Time.deltaTime * speed, touchDeltaPosition.y * Time.deltaTime * speed, 0);
		}
	}

Hierarchy> Cube> Inspector> TouchController.js setup public var speed:

> speed > movement
< speed < movement

By |Unity3D, Video Games Development|Commenti disabilitati su Unity 3D Game Engine – Android – Swipe – Screen – Move Object

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