• 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 /
avatar image
0
Question by James_BadAlchemy · Jan 29, 2017 at 09:08 AM · unity 2dspritesdrag and dropdrag objects

2D Drag And Drop Sprites Stalling?

So i'm working on being able to drag and drop sprites clean and efficiently. I've got it almost perfect, but when i drag one of my sprites on top of another 1 or 2 sprites (at a moderately reasonable speed) the sprite i was dragging stalls out and i have to touch it again to pick it back up. Also, one thing worth mentioning is that when a sprite is dropped, it should jump to its original starting position. This is feature functions correctly UNLESS i drag it through other sprites and it stalls.

TLDR; I can drag my sprites, nice and smooth no problems and when i lift my finger to release the sprite, it goes back to its designated home position. BUT when i drag my sprite through other sprites, it gets stuck and has to be manually moved away from other sprites in order to function correctly.

I've got 3 scripts you'll need to know about.

  • MasterScript, (Attatched to an empty gameobject called "Master") I'm mostly just using this to help store time sensitive/object specific variables.

    public class MasterScript : MonoBehaviour {

        public bool canDrag = true;
         public string currentTile = "";
     
     }
    
    
  • TouchDrag, This script is on EVERY sprite that i want to drag. It controls what happens when i touch is detected and directly references to this next script.

    public class TouchDrag : TouchSprite {

        Vector3 startPos;
         string ct;
         bool ms;
         public SpriteRenderer sp;
     
         void Start () {
             startPos = gameObject.transform.position;
             ms = GameObject.Find("Master").GetComponent<MasterScript>().canDrag;
             ct = GameObject.Find("Master").GetComponent<MasterScript>().currentTile;
         }
         
         void Update () {
             TouchInput(GetComponent<BoxCollider2D>());
         }
     
         void OnFirstTouchBegan() {
             if (ms) {
                 if (ct == "") { 
                 ms = false;
                 ct = gameObject.name;
                 sp.sortingOrder = 10;
                 }
             }
         }
     
         void OnFirstTouch() {
                 if (gameObject.name == ct) {
                     Vector3 pos;
                     pos = new Vector3(Camera.main.ScreenToWorldPoint(Input.GetTouch(0).position).x,
                     Camera.main.ScreenToWorldPoint(Input.GetTouch(0).position).y, transform.position.z);
                     transform.position = pos;
                 }
         }
     
        void OnFirstTouchEnded() {
             transform.position = startPos;
             ms = true;
             ct = "";
             sp.sortingOrder = 4;
         }
     }
    
    
  • TouchSprite, This script is all of the necessary framework for my previous TouchDrag script to function properly. It's pretty much just for defining when certain events and triggers occur.

    public class TouchSprite : MonoBehaviour {

        public static bool guiTouch = false;
     
         public void TouchInput(BoxCollider2D collider)
         {
             if (Input.touchCount > 0)
             {
                 if (collider == Physics2D.OverlapPoint(Camera.main.ScreenToWorldPoint(Input.GetTouch(0).position)))
                 {
                     switch (Input.GetTouch(0).phase)
                     {
                         case TouchPhase.Began:
                             SendMessage("OnFirstTouchBegan", SendMessageOptions.DontRequireReceiver);
                             SendMessage("OnFirstTouch", SendMessageOptions.DontRequireReceiver);
                             guiTouch = true;
                             break;
                         case TouchPhase.Stationary:
                             SendMessage("OnFirstTouchStayed", SendMessageOptions.DontRequireReceiver);
                             SendMessage("OnFirstTouch", SendMessageOptions.DontRequireReceiver);
                             guiTouch = true;
                             break;
                         case TouchPhase.Moved:
                             SendMessage("OnFirstTouchMoved", SendMessageOptions.DontRequireReceiver);
                             SendMessage("OnFirstTouch", SendMessageOptions.DontRequireReceiver);
                             guiTouch = true;
                             break;
                         case TouchPhase.Ended:
                             SendMessage("OnFirstTouchEnded", SendMessageOptions.DontRequireReceiver);
                             guiTouch = false;
                             break;
                     }
                 }
                 if (Input.touchCount > 1)
                 {
                     if (collider == Physics2D.OverlapPoint(Camera.main.ScreenToWorldPoint(Input.GetTouch(0).position)))
                     {
                         switch (Input.GetTouch(0).phase)
                         {
                             case TouchPhase.Began:
                                 SendMessage("OnSecondTouchBegan", SendMessageOptions.DontRequireReceiver);
                                 SendMessage("OnSecondTouch", SendMessageOptions.DontRequireReceiver);
                                 break;
                             case TouchPhase.Stationary:
                                 SendMessage("OnSecondTouchStayed", SendMessageOptions.DontRequireReceiver);
                                 SendMessage("OnSecondTouch", SendMessageOptions.DontRequireReceiver);
                                 break;
                             case TouchPhase.Moved:
                                 SendMessage("OnSecondTouchMoved", SendMessageOptions.DontRequireReceiver);
                                 SendMessage("OnSecondTouch", SendMessageOptions.DontRequireReceiver);
                                 break;
                             case TouchPhase.Ended:
                                 SendMessage("OnSecondTouchEnded", SendMessageOptions.DontRequireReceiver);
                                 break;
                         }
                     }
                 }
             }
         }
     }
    
    

I'd greatly appreciate it if you're able to help! Thanks it advance!!!

EDIT: I've continued working on this, and it seems like i might need to create a new base that detects when im already holding a sprite. I thought i could just do it through the DragSprite.cs but it's proving more and more difficult. Any help would be greatly appreciated!

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

· Add your reply
  • Sort: 
avatar image
0

Answer by James_BadAlchemy · Feb 01, 2017 at 03:23 AM

Any advice would be appreciated. Even a hint.

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

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

91 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

Related Questions

Sprite AI rapidly flipping to try and face the player. This is driving me insane. 0 Answers

My sprites are pixelated in the game view 0 Answers

Sprites Showing up Behind Tilemap??? 3 Answers

Cycling through array not working. Pulling my hair over this one. 0 Answers

Detect edge collision...? 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