• 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 · Show 1 · 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
avatar image AhmadYusaf · Jun 18, 2021 at 08:04 AM 0
Share

Can you please share your solution?

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

170 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 avatar image

Related Questions

Collition Coding not working 1 Answer

Moving moving Object with left/right arrow keys in a circular direction 3 Answers

Destroying 2D terrain at runtime 0 Answers

What is the most effective way to structure Card Effects in a Single Player game? 1 Answer

Randomize questions in a quiz game 2D 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges