0
\$\begingroup\$

I want to drag an object to a perticular place.In case I release that dragged object before I reach that particular area ,the object should come to its initial place where i start to drag. How it possible? Is there any specific motion for this?

void OnMouseDown()
{

    drag = true;
    //Debug.Log("haii ");
}
  void OnMouseDrag()
{
    if(drag)
    {
        var pos =Input.mousePosition;
        pos.x =Mathf.Clamp(pos.x,0,Screen.width);
        pos.y =Mathf.Clamp(pos.y,0,Screen.height);
        if(zdist>0)
        {
     zdist=Vector3.Distance(Camera.main.transform.position,transform.position);
            pos.z=zdist;
            pos = Camera.main.ScreenToWorldPoint(pos);
            transform.position =pos;

        }
    }
}
\$\endgroup\$

2 Answers 2

2
\$\begingroup\$

You can do this using plain mouseup/down tests and raycasting, and the same pattern applies to many other situations, like marquee selection.

// Call this inside an Update method.
void HandleDragging()
{
    const int mouseButton = 0; // button values are 0=left,1=right,2=middle
    if(_isDragging) // Leading underscore denotes private member variables
    {
        if(Input.GetMouseButton(mouseButton)) 
            UpdateDrag(); // Move your object according to Input.mousePosition
        if(Input.GetMouseButtonUp(mouseButton))
        {
            _isDragging = false;
            EndDrag(); // Set the object's position to its final destination
        }
    }
    else if (Input.GetMouseButtonDown(mouseButton))
    {
        // The bottom-left of the screen or window is at x:0, y:0
        var ray = Camera.main.ScreenPointToRay(Input.mousePosition);
        RaycastHit hit;
        // This requires a Collider component on the draggable objects
        if (Physics.Raycast(ray, out hit))
        {
            // Here you can filter down the kind of objects/components
            //     I've used gameObject, but transform is what you'll
            //     modify to move the object around.
            var dragObject = hit.transform.gameObject;
            _isDragging = true;
            // Here you can capture the object being dragged,
            //     or create a phantom representing the destination
            //     until the drag is completed or cancelled.
            BeginDrag(dragObject);
        }
    }
}
\$\endgroup\$
1
\$\begingroup\$

When you start drag (OnMouseDown), save object position in some variable. Then OnMouseUp you can test that object is in position you needed else restore object position by saved variable.

UPD:

// Define some target to place object in. It can be any Collider
public Collider target;
// Variable for saving initial object position
private Vector3 initialPosition;

void OnMouseDown()
{
    drag = true;
    // saving initial object position
    initialPosition = transform.position;
    //Debug.Log("haii ");
}

void OnMouseDrag()
{
    if(drag)
    {
        DragMe();
    }
}

void OnMouseUp()
{
    // UPD2: missed some test if object are actually dragging now
    if(drag) {
        // Testing object and target collision
       if(TestObjectInPlace(target, transform))
        {
            // Place object in target position if they are colliding
            transform.position = position;
            // or you can call DragMe() to put object under mouse cursor
            // DragMe();
        } else {
            // Restore initial object position if object is not collide with target
            transform.position = initialPosition;
        }
        drag = false;
    }
}

void DragMe()
{
    var pos =Input.mousePosition;
    pos.x =Mathf.Clamp(pos.x,0,Screen.width);
    pos.y =Mathf.Clamp(pos.y,0,Screen.height);
    if(zdist>0)
    {
        zdist=Vector3.Distance(Camera.main.transform.position,transform.position);
        pos.z=zdist;
        pos = Camera.main.ScreenToWorldPoint(pos);
        transform.position =pos;
    }
}

bool TestObjectInPlace(Collider target, Transform obj)
{
    // I test some distance between object and target for demo 
    // (I use distance less than 1 unit, but you can use any parameter of Collider
    // to test collision of target and object and if they are colliding return true 
    // or false if not)
    return Vector3.Distance(target.transform.position, obj.position) < 1;
}
\$\endgroup\$
2
  • \$\begingroup\$ error : Unexpected symbol `,' in class, struct, or interface member declaration bool TestObjectInPlace(Collider target, Transform object){ return Vector3.Distance(target.transform.position, object.position) < 1; } \$\endgroup\$ Commented Jul 21, 2014 at 9:01
  • \$\begingroup\$ Unfortunately I don't have access to Unity right now, so I cannot test it (but I do later). But you must understand only the basic idea: when you start drag save initial object position somewhere, then drag and when you drop object test if it is in right place and when it's not - restore initial position. \$\endgroup\$
    – lxmarduk
    Commented Jul 21, 2014 at 10:33

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .