• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
  • Help Room /
This question was closed May 15, 2016 at 03:01 PM by Miggarapo for the following reason:

The question is answered, right answer was accepted

avatar image
0
Question by Miggarapo · May 14, 2016 at 08:29 PM · c#2ddrag-and-dropdevice

Drag 2D Sprite by Touch

Im able to drag and drop gameobjects by touch. Well, here is an example script from the internet:

using UnityEngine; using System.Collections;

public class Drag2 : MonoBehaviour { private float dist; private bool dragging = false; private Vector3 offset; private Transform toDrag;

  void Update()
  {
      Vector3 v3;
 
      if (Input.touchCount != 1)
      {
          dragging = false;
          return;
      }
 
      Touch touch = Input.touches[0];
      Vector3 pos = touch.position;
 
      if (touch.phase == TouchPhase.Began)
      {
          RaycastHit hit;
          Ray ray = Camera.main.ScreenPointToRay(pos);
          if (Physics.Raycast(ray, out hit) && (hit.collider.tag == "Circle"))
          {
              Debug.Log("Here");
              toDrag = hit.transform;
              dist = hit.transform.position.z - Camera.main.transform.position.z;
              v3 = new Vector3(pos.x, pos.y, dist);
              v3 = Camera.main.ScreenToWorldPoint(v3);
              offset = toDrag.position - v3;
              dragging = true;
          }
      }
      if (dragging && touch.phase == TouchPhase.Moved)
      {
          v3 = new Vector3(Input.mousePosition.x, Input.mousePosition.y, dist);
          v3 = Camera.main.ScreenToWorldPoint(v3);
          toDrag.position = v3 + offset;
      }
      if (dragging && (touch.phase == TouchPhase.Ended || touch.phase == TouchPhase.Canceled))
      {
          dragging = false;
      }
  }

} Problem: i have no idea how to do that with Sprites which are 2D and have a Rigidbody2D and Collider2D. The above scripts needs mesh which is not for 2D. Any solutions ?

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

  • Sort: 
avatar image
0

Answer by Miggarapo · May 15, 2016 at 03:00 PM

Solved myself.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

169 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Why is my knockback direction script inconsistent with it's knockback direction? 1 Answer

Why won't my 2D Sprite Move? 1 Answer

Raycast on mouse position problem. how not to raycast to 0 1 Answer

How to make a thrown object land on a certain point e.g a thrown spear landing on its tip 0 Answers

Having a couple of problems regarding 2D Movement. 1 Answer

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges