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);
}
}
}
}
Attaching this Script to any GameObject
you control it to all directions, detail, you need to generate the APK
, because, note that has no mouse or keyboard control!
Take the tests, and it’s easy to implement the first part of the answer I posted!
Edit:
Specification of some functions used:
Input.Gettouch(0). Phase, The phase
is the state in which is the "touch" on the screen, the basic states are:
Touchphase.Began = Detects touch start position,
Touchphase.Moved = Tells where the touch has moved (in case of dragging),
Touchphase.Stationary = Informs where the touch was parked, that is, this holding the touch screen,
Touchphase.Ended = Informs the exact location of where the touch ended,
Touchphase.Canceled = When a ringtone is canceled, only in very specific cases.
Input.Gettouch(0). position, the position
returns the position x, y
and z
of objects or screen touch location.
Input.Gettouch (0). deltaPosition, deltaPosition returns a value of the type Vector2
which represents the difference of position between the current touch and the previous touch, and if you want to know the time between the two touches use:
deltaPosition.magnitude / Time.deltaTime
Follows Documentation for further clarification!
Good answer. But, to earn my +1, it would be important to add to it at least some explanations of how the code works (because even though the code is reusable, someone with little experience might have great difficulty understanding its functioning). For example, you can at least cite what certain functions like
Input.GetTouch (0).phase
orInput.GetTouch (0).deltaPosition
, among others.– Luiz Vieira
@Luizvieira follows edition of the answer, very good to know that you are interested in such subject! I promise to improve in these details!
– Junior Moreira
Oops! Now yes! + 1 :)
– Luiz Vieira
@Júniormoreira Thanks :D the code worked, however it is turning the character 2d instead of going sideways, I recorded a video to see better how it looked https://www.youtube.com/watch?v=BZ3GoM8MUhY
– Matheus Muriel
@Matheusmuriel, change the
transform.Rotate
fortransform.Position
if you have any questions and I am not here you can consult http://docs.unity3d.com/ScriptReference/Transform-position.html– Junior Moreira
@Junior I tried to change to Transform.Position, but he was wrong. and when the error came out the character just jumps.
– Matheus Muriel
I’ll post the code here in the answers
– Matheus Muriel