Drive with TouchScrren 2D

2

I need to make a C # Script code for moving a 2d character on Unity.

I developed a code that runs well on the pc using the arrows, but I have no idea how to move it using touchScren from an Android phone.

Here's what I've done:

using UnityEngine;
using System.Collections;

public class Player : MonoBehaviour
   {

public float velocidade;
public float forcaPulo;
private bool estaNoChao;
public Transform chaoVerificador;

// Use this for initialization
void Start()
{

}

// Update is called once per frame
void Update()
{
    Movimentacao();
}

void Movimentacao()
{

    estaNoChao = Physics2D.Linecast(transform.position, chaoVerificador.position, 1 << LayerMask.NameToLayer("Piso"));
    if (Input.GetAxis("Horizontal") > 0)
    {
        transform.Translate(Vector2.right * velocidade * Time.deltaTime);
        transform.eulerAngles = new Vector2(0, 0);
    }

    if (Input.GetAxis("Horizontal") < 0)
    {
        transform.Translate(Vector2.right * velocidade * Time.deltaTime);
        transform.eulerAngles = new Vector2(0, 180);
    }


}
}
    
asked by anonymous 12.02.2016 / 06:33

2 answers

2

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!

    
12.02.2016 / 09:43
0
using UnityEngine;
using System.Collections;

public class Player : 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.position = new Vector3();
        }
        else if (touch.x < Screen.width / 2 && touch.y > Screen.height / 4 && touch.y < (Screen.height - (Screen.height / 4)))
        {
            Debug.Log("Para Esquerda");
            transform.position = new Vector3();
        }
        else if (touch.y > Screen.height - (Screen.height / 4))
        {
            Debug.Log("Para cima");
            transform.position = new Vector3();
        }
        else
        {
            Debug.Log("Para Direita");
            transform.position= new Vector3();
        }
    }
}

}

    
14.02.2016 / 19:30