Basic script to facilitate understanding:
using UnityEngine;
using System.Collections;
public class MoveTouch : MonoBehaviour
{
private float speed = 1.5f;
//Controle de Zoon
private Vector2 v2_current_Distance;
private Vector2 v2_previous_Distance;
private float f_comfort_zone;
private float mScaleFactor = 1;
private GameObject go;
//Controle de Rotaçao
private Vector2 firstPressPos;
private Vector2 secondPressPos;
private Vector2 currentSwipe;
void Start ()
{
go = GameObject.Find ("target");
}
void Update ()
{
if (Input.touchCount == 2 && Input.GetTouch (0).phase == TouchPhase.Moved && Input.GetTouch (1).phase == TouchPhase.Moved) {
v2_current_Distance = Input.GetTouch (0).position - Input.GetTouch (1).position;
v2_previous_Distance = ((Input.GetTouch (0).position - Input.GetTouch (0).deltaPosition) - (Input.GetTouch (1).position - Input.GetTouch (1).deltaPosition));
//Funçao Zoon
float touchDelta = v2_current_Distance.magnitude - v2_previous_Distance.magnitude;
if (touchDelta <= 1) {
mScaleFactor = transform.localScale.x;
mScaleFactor *= 0.9f;
transform.localScale = new Vector3 (mScaleFactor, mScaleFactor, mScaleFactor);
}
if (touchDelta > 1) {
mScaleFactor = transform.localScale.x;
mScaleFactor *= 1.1f;
transform.localScale = new Vector3 (mScaleFactor, mScaleFactor, mScaleFactor);
}
} else if (Input.touchCount == 1 && Input.GetTouch (0).phase == TouchPhase.Moved) {
Vector2 touchDeltaPosition = Input.GetTouch (0).deltaPosition;
go.transform.Translate (touchDeltaPosition.x * speed * Time.deltaTime, touchDeltaPosition.y * speed * Time.deltaTime, touchDeltaPosition.y * speed * Time.deltaTime);
} else if (Input.touchCount == 1 && Input.GetTouch (0).phase == TouchPhase.Stationary) {
Vector2 touch = Input.GetTouch (0).position;
if (touch.y < Screen.height / 4) {
Debug.Log ("Para Baixo");
transform.Rotate (Vector3.right, 2);
} else if (touch.x < Screen.width / 2 && touch.y > Screen.height / 4 && touch.y < (Screen.height - (Screen.height / 4))) {
Debug.Log ("Para Esquerda");
transform.Rotate (Vector3.up, 2);
} else if (touch.y > Screen.height - (Screen.height / 4)) {
Debug.Log ("Para cima");
transform.Rotate (Vector3.left, 2);
} else {
Debug.Log ("Para Direita");
transform.Rotate (Vector3.down, 2);
}
}
}
}
By attaching this script to any GameObject
you will control it for all directions, detail, you need to generate APK
, so note that you do not have any mouse or keyboard controls!
Take the test, and it's easy to implement the first part of the answer I posted!
Edit:
Specification of some functions used:
Input.GetTouch (0) .phase , phase
is the state that the touch is on the screen, the basic states are:
TouchPhase.Began = Detects touch start position,
TouchPhase.Moved = Tells where the touch moved (in cases of drag),
TouchPhase.Stationary = Informs you where the ringing has been parked, ie holding the touch on the screen,
TouchPhase.Ended = Tells you where exactly the touch has ended,
TouchPhase.Canceled = When a ringtone is canceled, only in very specific cases.
Input.GetTouch (0) .position , position
returns the position x, y
and z
of objects or touch location on the screen.
Input.GetTouch (0) .deltaPosition , deltaPosition returns a value of type Vector2
that represents the difference of position between the current touch and previous touch, and if you want to know the time between the two touches use:
deltaPosition.magnitude / Time.deltaTime
Follow Documentation for further clarification!