programming

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 – 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 – 3D Platform – Smoot Follow

Unity 3D Game Engine – Camera – 3D Platform – Smoot Follow

1. Inside Hierarchy create the structure:

– Player (parent)
— CameraTarget (Empty Object) (child) -> posizionarlo in alto dietro il Player (con un valore di Z più piccolo)

– Main Camera

2. Select ‘Main Camera’ -> ruotarla leggermente verso il basso per inquadrare il player 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 cameraFollowZ : boolean = true;        // Inspector> if is checked -> The Camera will follow Z position of cameraTarget
var cameraFollowHeight : boolean = false;  // if true the Camera Y Position = cameraHeight
var cameraHeight : float = 2.5;            // cameraHeight
var velocity : Vector3;
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 (cameraFollowZ) // if cameraFollowZ = true = Inspector is checked
{
  thisTransform.position.z = Mathf.SmoothDamp (thisTransform.position.z, cameraTarget.transform.position.z, velocity.z, 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 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 CameraTarget 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

d. Camera Follow Z check
-> follow Z of CameraTarget

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

Unity 3D Game Engine – JS Script – Follow the Player

Unity 3D Game Engine – JS Script – Follow the Player

1. MAIN TOP MENU> Game Object> Create Other> Cube> rename it ‘Player’

2. Hierarchy> Select ‘Main Camera’> Inspector> ‘Add Component’> New script> ‘CameraController.js’

CameraController.js:


#pragma strict
 
//The GameObject the Camera will follow 
public var player : GameObject;
//The actual Camera Position
private var offset :  Vector3;
 
function Start () {
offset = transform.position;
}
 
function LateUpdate () {
//LateUpdate is called after all Update functions have been called. 
// This is useful to order script execution. For example a follow camera should always be implemented in LateUpdate because it tracks objects that might have moved inside Update.
// The camera moves player.position+his own position
transform.position = player.transform.position + offset;
}

3. Hierarchy> ‘Main Camera’> Inspector> LookAt.js> DRAG ANd DROP over puiblic variable ‘Player’> the ‘Player’ object you have created at point 1.

4. PLAY the game, while playing from Inspector change ‘Player’ object position to see the final result.

By |Unity3D, Video Games Development|Commenti disabilitati su Unity 3D Game Engine – JS Script – Follow the Player