Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Multi Level Drag and Drop problem.

Discussion in 'Scripting' started by kodarr, Jul 30, 2022.

  1. kodarr

    kodarr

    Joined:
    Sep 27, 2021
    Posts:
    8
    Ok I've looked all over but not sure how to even word this to find an answer and unsure why this is happening but I have a sprite background of a table top. I am using the following code on this object.
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class DragDropRestricted : MonoBehaviour
    6. {
    7.     Vector3 mousePosOffset;
    8.     public float xMax =10;
    9.     public float xMin =-10;
    10.     public float yMax =10;
    11.     public float yMin =-10;
    12.  
    13.     private Vector3 GetMouseWorldPosition()
    14.     {
    15.         //capture mouse position & return worldpoint
    16.         return Camera.main.ScreenToWorldPoint(Input.mousePosition);
    17.     }
    18.  
    19.     private void OnMouseDown()
    20.     {
    21.         //capture mouse offset
    22.         mousePosOffset = gameObject.transform.position - GetMouseWorldPosition();
    23.     }
    24.  
    25.     private void OnMouseDrag()
    26.     {
    27.         transform.position = GetMouseWorldPosition() + mousePosOffset;
    28.         if (transform.position.x>xMax)
    29.         {
    30.             transform.position = new Vector3(xMax, transform.position.y, transform.position.z);
    31.         }
    32.         else if (transform.position.x < xMin)
    33.         {
    34.             transform.position = new Vector3(xMin, transform.position.y, transform.position.z);
    35.         }
    36.  
    37.         if (transform.position.y > yMax)
    38.         {
    39.             transform.position = new Vector3(transform.position.x, yMax, transform.position.z);
    40.         }
    41.         else if (transform.position.y < yMin)
    42.         {
    43.             transform.position = new Vector3(transform.position.x, yMin, transform.position.z);
    44.         }
    45.     }
    46. }
    47.  
    This works fine and clamps the image so I can scroll it out of bounds. Now the problem is my program will instantiate tokens on the table that will move when the table moves. This works by having the spawn point being a child of the table.
    Here's where the problem comes in. When I first put the token on the table I can drag them around just fine. Once I move the table once I can no longer drag the tokens and it just drags the table now instead. I have no idea why this is the case. I've tried multiple ways of doing the drag and drop on the tokens and the table top but nothing has helped.
    Here are 2 of the ways I have tried on the tokens
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class DragAndDropNew : MonoBehaviour
    6. {
    7.     private float startPosX;
    8.     private float startPosY;
    9.     private bool isBeingHeld = false;
    10.  
    11.     private void Update()
    12.     {
    13.         if (isBeingHeld)
    14.         {
    15.             Vector2 mousePos;
    16.             mousePos = Input.mousePosition;
    17.             mousePos = Camera.main.ScreenToWorldPoint(mousePos);
    18.  
    19.             this.gameObject.transform.localPosition = new Vector3(mousePos.x-startPosX , mousePos.y-startPosY , 0);
    20.         }
    21.     }
    22.     private void OnMouseDown()
    23.     {
    24.         if (Input.GetMouseButtonDown(0))
    25.         {
    26.             Vector2 mousePos;
    27.             mousePos = Input.mousePosition;
    28.             mousePos = Camera.main.ScreenToWorldPoint(mousePos);
    29.  
    30.             startPosX = mousePos.x - this.transform.localPosition.x;
    31.             startPosY = mousePos.y - this.transform.localPosition.y;
    32.  
    33.             isBeingHeld = true;
    34.         }
    35.  
    36.     }
    37.  
    38.     private void OnMouseUp()
    39.     {
    40.             isBeingHeld = false;
    41.     }
    42. }
    43.  
    and
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class DragDrop : MonoBehaviour
    6. {
    7.     Vector3 mousePosOffset;
    8.  
    9.     private Vector3 GetMouseWorldPosition()
    10.     {
    11.         //capture mouse position & return worldpoint
    12.         return Camera.main.ScreenToWorldPoint(Input.mousePosition);
    13.     }
    14.  
    15.     private void OnMouseDown()
    16.     {
    17.         //capture mouse offset
    18.         mousePosOffset = gameObject.transform.position - GetMouseWorldPosition();
    19.     }
    20.  
    21.     private void OnMouseDrag()
    22.     {
    23.         transform.position = GetMouseWorldPosition() + mousePosOffset;
    24.     }
    25. }
    26.  
    Any help would be greatly welcome. This seems like such an easy concept yet I have no clue why it isn't working.
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,947
    Well, figure that out first... welcome to debugging!! Here's how:

    You must find a way to get the information you need in order to reason about what the problem is.

    What is often happening in these cases is one of the following:

    - the code you think is executing is not actually executing at all
    - the code is executing far EARLIER or LATER than you think
    - the code is executing far LESS OFTEN than you think
    - the code is executing far MORE OFTEN than you think
    - the code is executing on another GameObject than you think it is
    - you're getting an error or warning and you haven't noticed it in the console window

    To help gain more insight into your problem, I recommend liberally sprinkling Debug.Log() statements through your code to display information in realtime.

    Doing this should help you answer these types of questions:

    - is this code even running? which parts are running? how often does it run? what order does it run in?
    - what are the values of the variables involved? Are they initialized? Are the values reasonable?
    - are you meeting ALL the requirements to receive callbacks such as triggers / colliders (review the documentation)

    Knowing this information will help you reason about the behavior you are seeing.

    You can also supply a second argument to Debug.Log() and when you click the message, it will highlight the object in scene, such as
    Debug.Log("Problem!",this);


    If your problem would benefit from in-scene or in-game visualization, Debug.DrawRay() or Debug.DrawLine() can help you visualize things like rays (used in raycasting) or distances.

    You can also call Debug.Break() to pause the Editor when certain interesting pieces of code run, and then study the scene manually, looking for all the parts, where they are, what scripts are on them, etc.

    You can also call GameObject.CreatePrimitive() to emplace debug-marker-ish objects in the scene at runtime.

    You could also just display various important quantities in UI Text elements to watch them change as you play the game.

    If you are running a mobile device you can also view the console output. Google for how on your particular mobile target, such as this answer or iOS: https://forum.unity.com/threads/how-to-capturing-device-logs-on-ios.529920/ or this answer for Android: https://forum.unity.com/threads/how-to-capturing-device-logs-on-android.528680/

    Another useful approach is to temporarily strip out everything besides what is necessary to prove your issue. This can simplify and isolate compounding effects of other items in your scene or prefab.

    Here's an example of putting in a laser-focused Debug.Log() and how that can save you a TON of time wallowing around speculating what might be going wrong:

    https://forum.unity.com/threads/coroutine-missing-hint-and-error.1103197/#post-7100494
     
  3. kodarr

    kodarr

    Joined:
    Sep 27, 2021
    Posts:
    8
    I had debugs riddled through it that I removed from the code since they didn't tell me much more than I already knew. I know what is triggering when from what is working and what isn't triggering.

    I don't have any idea what to look for debug wise where sometimes it works and sometimes it doesn't. All I know is the background always works but sometimes dragging a token moves the background instead of the token but not always. I checked the box collider is always there. The tokens always show on the screen so they aren't behind the background I they are raycast targets so you can't click anything behind them.

    I don't need to find the solution just some ideas on what to look for why clicking the tokens would think I'm clicking the object below them instead when I go to drag them.

    I created code to tell me when an object is clicked to see but as I expected when the background is moving and not the token the token appears as not being clicked. I'm not sure why after awhile you can no longer click the tokens but they work at first.
     
    Last edited: Jul 31, 2022