Drag and Drop Unity

1

I am starting with Unity in the development of a simulator for Smartphones for both Android and Ios, it is basically a drag and drop of objects to simulate actions. I followed some tutorials of movement of objects and actions but I need this to be touch and from that I could not find anything. Well all this I want to be in a 2D environment, and for the object I use the properties of RigidBody 2D and CirculeCollider 2D

using UnityEngine;
using System.Collections;
using UnityEngine.UI;

public class PlayerController : MonoBehaviour {

    public float speed;
    public Text counText; 
    public Text Wintext;

    private Rigidbody2D rb2d;
    private int count;



    void Start()
    {
        rb2d = GetComponent<Rigidbody2D> ();
        count = 0;
        SetCountText ();
        Wintext.text = "";
    }



    void FixedUpdate ()
    {
        float moveHorizontal = Input.GetAxis ("Horizontal");
        float moveVertical = Input.GetAxis ("Vertical");
        Vector2 movment = new Vector2 (moveHorizontal, moveVertical);
        rb2d.AddForce (movment * speed);
    } 


    void OnTriggerEnter2D(Collider2D other)
    {
        if (other.gameObject.CompareTag ("PickUp"))
        {
            other.gameObject.SetActive (false);
            count = count + 1;
            SetCountText ();
        }
    }

    void SetCountText ()
    {
        counText.text = "count: " + count.ToString ();
        if (count >= 7) 
        {
            Wintext.text = "you win!";
        }   
    }
}

So far that's what I have for the movement but how can I do it to recognize the touch of cell phones?

    
asked by Nicolas Rudisky 29.07.2016 в 16:55
source

1 answer

1

You can use as you well said "Touch", here is a small example taken from the Unity API:

if (Input.touchCount > 0 && Input.GetTouch(0).phase == TouchPhase.Moved) {

    // touchCount devuelve el número de dedos sobre la pantalla
    // GetTouch(0).phase devuelve el estado del primer dedo registrado
}

You just have to read the API a bit to find more information about the "Touch" and its phases. Here are links from the Unity API that will help you:

touchCount

GetTouch

Investigate this and if you see that even then you do not clarify I try to help you.

[EDITED]

private bool isDragging;

void Start() {

  isDragging = false;
}

void Update() {

  if (Input.touchCount != 1) { isDragging = false; }

  if (!isDragging) {

    // tu código para coger un objeto
    isDragging = true;
  }
}
    
answered by 02.08.2016 / 13:58
source