Search Unity

Force a block to drop when being dragged over hole

Discussion in '2D' started by TazmanNZL, May 16, 2014.

  1. TazmanNZL

    TazmanNZL

    Joined:
    May 10, 2014
    Posts:
    4
    I'm working on a block game and I am trying to figure out how to have a block drop when it is moved over a missing block. I can't seem to find anything in code that cancels the dragging. Here are the functions for dragging:

    Code (csharp):
    1. void OnMouseDown()
    2.     {
    3.         screenPoint = Camera.main.WorldToScreenPoint(gameObject.transform.position);
    4.        
    5.         offset = gameObject.transform.position - Camera.main.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, gameObject.transform.position.y, screenPoint.z));
    6.     }
    7.    
    8.     void OnMouseDrag()
    9.     {
    10.         Vector3 curScreenPoint = new Vector3(Input.mousePosition.x, gameObject.transform.position.y, screenPoint.z);
    11.        
    12.         Vector3 curPosition = Camera.main.ScreenToWorldPoint(curScreenPoint)+offset;
    13.         transform.position = curPosition;
    14.        
    15.     }