Drive with 2D Touchscrren

Asked

Viewed 1,012 times

2

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

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

Follow 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);
    }


}
}

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);
            }            
        }        
    }
}

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 or Input.GetTouch (0).deltaPosition, among others.

  • 1

    @Luizvieira follows edition of the answer, very good to know that you are interested in such subject! I promise to improve in these details!

  • Oops! Now yes! + 1 :)

  • @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

  • @Matheusmuriel, change the transform.Rotate for transform.Position if you have any questions and I am not here you can consult http://docs.unity3d.com/ScriptReference/Transform-position.html

  • @Junior I tried to change to Transform.Position, but he was wrong. and when the error came out the character just jumps.

  • I’ll post the code here in the answers

Show 2 more comments

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();
        }
    }
}

}

Browser other questions tagged

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