How to make a drive system using touch in unity?

8

When you slide your finger up screen performs the command and when you slide your finger down the command executes, an example of a command would be a move up and a move down with the touch me returning a type bool co_de % or% with%.

    
asked by anonymous 13.12.2015 / 19:44

1 answer

7

You can test positions:

        //Variáveis de controle
        bool up    = false;
        bool down  = false;
        bool left  = false;
        bool right = false; 

        if (touch.y < Screen.height / 4) {
            Debug.Log ("Para Baixo"); //Aqui você seta a variável para TRUE
            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"); //Aqui você seta a variável para TRUE
            transform.Rotate (Vector3.up, 2);
        }  else if (touch.y > Screen.height - (Screen.height / 4)) {
            Debug.Log ("Para cima"); //Aqui você seta a variável para TRUE
            transform.Rotate (Vector3.left, 2);
        }  else {
            Debug.Log ("Para Direita"); //Aqui você seta a variável para TRUE
            transform.Rotate (Vector3.down, 2);
        } 

When moving to any direction you have the variable True , you can still implement to know if you are moving to the diagonals, just test the 2 displacements simultaneously!

Edited to your question edit

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!

    
13.12.2015 / 20:48