Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

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;

        }
    }
}
share|improve this question

2 Answers 2

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);
        }
    }
}
share|improve this answer

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;
}
share|improve this answer
    
can u provide me a sample code that how does it works? –  Deepika C P Jul 21 at 6:48
    
This sample is common case for inventing your own drag'n'drop system. But I didn't find proper events for "drag end" and "drag cancel" in Unity documentation. –  lxmarduk Jul 21 at 8:22
    
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; } –  Deepika C P Jul 21 at 9:01
    
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. –  lxmarduk Jul 21 at 10:33
1  
Here the working sample. –  lxmarduk Jul 21 at 16:39

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.