Skip to main content
Tweeted twitter.com/#!/StackGameDev/status/499955911751110656

I want to drag an object to a perticular place.In case i leftI release that draggingdragged object before iI reach that perticularparticular area i want to be that,the object in the sameshould come to its initial placeplace 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;
            
        }
    }
}

I want to drag an object to a perticular place.In case i left that dragging object before i reach that perticular area i want to be that object in the same 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;
            
        }
    }
}

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;
            
        }
    }
}
Source Link
Deepika C P
  • 107
  • 4
  • 17

Drag an object in unity3d

I want to drag an object to a perticular place.In case i left that dragging object before i reach that perticular area i want to be that object in the same 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;
            
        }
    }
}