Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Stop Drag Drop sprite going over another object

Discussion in 'Scripting' started by ds223, Jul 17, 2019.

  1. ds223

    ds223

    Joined:
    Nov 5, 2018
    Posts:
    8
    I have added drag drop to a sprite, I have put some code in that when the ball sprite drags over another object it will jump back to it's previous position.

    Is there anyway to stop the ball being dragged over the other object in the first place, or a better way of adding drag drop.

    Code (CSharp):
    1.     public void OnDrag(PointerEventData eventData)
    2.     {
    3.         var newPosition = Camera.main.ScreenToWorldPoint(eventData.position);
    4.  
    5.         RaycastHit2D hit = Physics2D.Raycast(newPosition, Vector2.zero);
    6.         var okToMove = true;
    7.  
    8.         if (hit.collider != null)
    9.         {
    10.             okToMove = (hit.collider.gameObject == gameObject);
    11.         }
    12.        
    13.         if (okToMove)
    14.         {
    15.             transform.localPosition = new Vector2(newPosition.x, newPosition.y);
    16.         }
    17.     }
    Thanks
     

    Attached Files:

  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,520
    Generally yes, there are many ways. You need to define the behavior you want before you can implement it.

    For instance, you could do any one of a number of things when something is incorrectly dragged "over" another object:

    - instantly reset the object back to its pre-drag location
    - destroy the object irreparably
    - cause the object to "snag" on the edge of the forbidden zone while the mouse goes on
    - try and back-drive the mouse pointer so it too freezes if you go near the forbidden zone
    - define some kind of "tween away" behavior that causes the dragged object to deflect from the forbidden area
    - additionally back-drive the mouse pointer away to match the tweened-away object's location

    The above are listed in roughly the estimated order of ease of implementation. Your mileage may vary. Give a few of them a shot and report back!
     
  3. ds223

    ds223

    Joined:
    Nov 5, 2018
    Posts:
    8
    Thanks for the response.

    This is what my code was trying to do but did not stop the object being dragged over it before moving it back.

    I will have a look at the other points and try find some information about them to see if they will help
     
  4. ds223

    ds223

    Joined:
    Nov 5, 2018
    Posts:
    8
    I have changed the code to this which seems to stop the object going over another one.

    Issue now is that the sprite continues to bounce on the object it is colliding with if the drag is kept held down by the user and after a while just disappears off the screen.

    So not sure if the fix I have is actually the correct approach or just needs a bit more work.

    Code (CSharp):
    1.  
    2. private Rigidbody2D rigidbody;
    3.  
    4. void Start()
    5. {
    6.         rigidbody = GetComponent<Rigidbody2D>();
    7. }
    8.  
    9. public void OnDrag(PointerEventData eventData)
    10. {
    11.         var newPosition = Camera.main.ScreenToWorldPoint(eventData.position);
    12.  
    13.         rigidbody.velocity = (newPosition - transform.position) * 10;
    14. }
    15.  
    16.    public void OnEndDrag(PointerEventData eventData)
    17. {
    18.         rigidbody.velocity = Vector2.zero;
    19. }
    20.  
     
    Last edited: Aug 13, 2019
  5. ds223

    ds223

    Joined:
    Nov 5, 2018
    Posts:
    8
    Hopefully this animated gif will show the issue that is happening to the sprite.

    I drag the sprite around and then hold it on the bottom of the screen, then it starts to bounce then fires off the screen

    output.gif