• Unity
  • Services
  • Made with Unity
  • Learn
  • 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
  • Forums
  • Answers
  • Feedback
  • Issue Tracker
  • Blog
  • Evangelists
  • User Groups

Navigation

  • Home
  • Unity
  • Industries
  • Made with Unity
  • Learn
  • Community
    • Forums
    • Answers
    • Feedback
    • Issue Tracker
    • Blog
    • Evangelists
    • User Groups
  • Get Unity
  • Asset Store

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 · spritesunity 2ddrag 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

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

95 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

Related Questions

Sprites have discolored patches / color bleeding 0 Answers

Tips of Reducing the size of Build? 2 Answers

Texture count in Profiler increasing everytime animation is played 0 Answers

How to check if dragged object collide with other object? 0 Answers

Animation : how to Interrupt an animation sometimes ? 0 Answers

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