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

Drag&Drop pictures under mask

Discussion in 'Scripting' started by T0RED, Jan 10, 2022.

  1. T0RED

    T0RED

    Joined:
    Oct 3, 2018
    Posts:
    6
    Hello together,
    I want to move the pictures under masks per drag&drop (see image below ). So over the mask1 I want to move picture1; over the mask2 I want to move picture 2 and so on. But my problem is when I want to move picture 3 under mask3, I am moving picture4 because its overlapping with picture3.

    Does anyone know a solution?

    With best regards
    Tobi

    Masken-Problem.jpg
     
  2. GroZZleR

    GroZZleR

    Joined:
    Feb 1, 2015
    Posts:
    3,201
    So how do you determine which one is the "correct" picture to move, purely through the mask? So Picture3 will always be the one that moves if you're mousing over Mask3? If that's the case, keep track of a list of what picture goes with what mask, and then call the appropriate dragging functions directly.
     
    T0RED likes this.
  3. T0RED

    T0RED

    Joined:
    Oct 3, 2018
    Posts:
    6
    -> yes

    -> That sounds like a good solution, but I've also found another solution that works well for me right now. I am using a raycast in my OnMouseDown() method:

    Code (CSharp):
    1. public void OnMouseDown()
    2. {
    3.         RaycastHit rayhit;
    4.         Vector3 realMousePosition = Input.mousePosition;
    5.         realMousePosition.y = Screen.height - realMousePosition.y;
    6.         Ray ray = Camera.main.ScreenPointToRay(realMousePosition);
    7.         if (Physics2D.Raycast(transform.position, realMousePosition))
    8.         {
    9.             isDragging = true;
    10.         }
    11. }
    In OnMouseUp() I set isDragging to false and in Update() I am moving the gameobject, when isDragging is true.

    But anyway thank you very much for your answer.
     
    GroZZleR likes this.