How to make a motion system using touch in Unity?

Asked

Viewed 3,327 times

9

When sliding your finger up screen executes the command and when sliding your finger down executes the command, an example of command would be a move up and a move down with the touch me return a type bool true or false.

2 answers

8


You can test the 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, it is still possible to implement to know if this moving to the diagonals, just test the 2 displacements simultaneously!

Edited as per your question

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!

  • i tested, and did not work in case would have to pass a parameter to recognize this move on the screen.

  • Actually, buddy, this test is for exactly what you described in the question, not motion control. Serves as you already have a system of movement, which implied in your question.

  • I think I expressed myself badly, I wanted to implement touch in Unity because I don’t have a drive system, if you think better I open another question and leave this as a test of movement.

  • 1

    You didn’t have to open another question, just edit this one!

  • OK I’ll edit this worth

0

If you prefer to use as the directional input smoothly, the intensity you can change according to the screen size so when you get to y = -1 or 1 would assign the value to top and down and similar to x left and right,

    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;

    public class Usatouch : MonoBehaviour
    {
        private Vector2 StartPos ;
        private Vector2 TouchDir ;
        public int intensidade = 200; 
        void Update()
        {
            touchupdate();
            horizontal = Mathf.Clamp(TouchDir.x/intensidade,-1,1);
            vertical = Mathf.Clamp(TouchDir.y/intensidade,-1,1);

        }
        void touchupdate()
        {
            if (Input.touchCount > 0)
            {
                Touch touch = Input.GetTouch(0);
                switch(touch.phase)
                {
                case TouchPhase.Began:
                    StartPos = touch.position;  
                    break;
                case TouchPhase.Moved:
                    TouchDir = touch.position - StartPos;
                    break;
                case TouchPhase.Ended:  
                    break;
                }
            }
        }

}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.