Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

how can i drag and objects with mouse click

Discussion in '2D' started by sharpg, Mar 13, 2015.

  1. sharpg

    sharpg

    Joined:
    Mar 10, 2015
    Posts:
    150
    Dear All,
    My object is simple sprite. how can i drag that object with mouse?
    and in need when we do mouse up object return back to its original position.
     
  2. sluice

    sluice

    Joined:
    Jan 31, 2014
    Posts:
    416
    Put a BoxCollider2D on your GameObject.

    Then, create a new C# script, like so:

    Code (csharp):
    1.  
    2. using UnityEngine;
    3. using System.Collections;
    4.  
    5. public class DragScript : MonoBehaviour
    6. {
    7.     bool isDragged = false;
    8.  
    9.     void OnMouseDown()
    10.     {
    11.         isDragged = true;
    12.     }
    13.  
    14.     void OnMouseUp()
    15.     {
    16.         isDragged = false;
    17.     }
    18.    
    19.     void Update ()
    20.     {
    21.         if(!isDragged)
    22.             return;
    23.  
    24.         Vector2 currentScreenPoint = new Vector2(Input.mousePosition.x, Input.mousePosition.y);
    25.         Vector2 currentPos = Camera.main.ScreenToWorldPoint(currentScreenPoint);
    26.        
    27.         transform.position = new Vector3(currentPos.x, currentPos.y, transform.position.z);
    28.     }
    29. }
    30.  
     
    Damink37 and sharpg like this.
  3. sharpg

    sharpg

    Joined:
    Mar 10, 2015
    Posts:
    150
    Thanks :).
     
    Last edited: Mar 14, 2015
  4. nikkouskouk

    nikkouskouk

    Joined:
    Jan 23, 2015
    Posts:
    29
    there you go .

    Just follow the tutorials.
     
  5. sharpg

    sharpg

    Joined:
    Mar 10, 2015
    Posts:
    150
    i have made addition to this code
    1. void OnMouseUp()
    2. {
    3. isDragged = false;
    4. transform.position=newVector3(0.82f,-3.50f,0.0f);
    5. }
    this makes my object to jump on its original position when i do mouse up.
    now i want to control the speed of object when it moves towards original potion on mouse up.please help.
    thanks :)
     
    Last edited: Mar 14, 2015
  6. sharpg

    sharpg

    Joined:
    Mar 10, 2015
    Posts:
    150
    Thanks:)
     
  7. sluice

    sluice

    Joined:
    Jan 31, 2014
    Posts:
    416
    Instead, I would modify the code like so:

    Code (csharp):
    1.  
    2. Vector3 startPos;
    3.  
    4. void OnMouseDown()
    5. {
    6.     isDragged = true;
    7.     startPos = transform.position;
    8. }
    9. void OnMouseUp()
    10. {
    11.     isDragged = false
    12.     transform.position = startPos;
    13. }
    14.  
    By doing so,
    1- You don not have to define the return position by hand, like so: transform.position=newVector3(0.82f,-3.50f,0.0f);
    So this script can easily be used on any object.

    2- If the position of your object changes for any reason, the script will still work.
     
  8. sluice

    sluice

    Joined:
    Jan 31, 2014
    Posts:
    416
    And if you want to have a return speed, I would Lerp the position inside a coroutine.

    Something like this (not tested)

    Code (csharp):
    1.  
    2. using UnityEngine;
    3. using System.Collections;
    4.  
    5. public class DragScript : MonoBehaviour
    6. {
    7.     public float returnSpeed = 5f;
    8.     bool isDragged = false;
    9.     Vector3 startPos;
    10.    
    11.     void OnMouseDown()
    12.     {
    13.         startPos = transform.position;
    14.         isDragged = true;
    15.     }
    16.  
    17.     void OnMouseUp()
    18.     {
    19.         isDragged = false;
    20.         StartCoroutine("ReturnToStartPosition");
    21.     }
    22.  
    23.     void Update ()
    24.     {
    25.         if(!isDragged)
    26.             return;
    27.    
    28.         Vector2 currentScreenPoint = new Vector2(Input.mousePosition.x, Input.mousePosition.y);
    29.         Vector2 currentPos = Camera.main.ScreenToWorldPoint(currentScreenPoint);
    30.    
    31.         transform.position = new Vector3(currentPos.x, currentPos.y, transform.position.z);
    32.     }
    33.  
    34.     IEnumerator ReturnToStartPosition()
    35.     {
    36.         while(Vector2.SqrMagnitude(transform.position - startPos) >= 0.1f)
    37.         {
    38.             transform.position = Vector2.Lerp(transform.position, startPos, returnSpeed * Time.deltaTime);
    39.             yield return 0;
    40.         }
    41.     }
    42. }
    43.  
    EDIT: Mistake on my part, comparing two vectors like this is a bad approach. Using the Vector2.SqrMagnitude makes the most sens, in my opinion.
     
    Last edited: Mar 14, 2015
  9. sharpg

    sharpg

    Joined:
    Mar 10, 2015
    Posts:
    150
    Kindly tell me guys waht is the purpose of "Vector2 dir = Vector2.zero;" in this code?
    usingUnityEngine;
    usingSystem.Collections;

    publicclassMouseManager : MonoBehaviour {


    //Updateiscalledonceperframe
    voidUpdate () {
    if(Input.GetMouseButtonDown(0)){
    Vector3mouseWorldPos3D = Camera.main.ScreenToWorldPoint (Input.mousePosition);
    Vector2mousePos2D= newVector2 (mouseWorldPos3D.x,mouseWorldPos3D.y);
    Debug.Log(mousePos2D);
    Vector2 dir = Vector2.zero;
    RaycastHit2Dhit = Physics2D.Raycast(mousePos2D,dir);
    if (hit!=null && hit.collider!=null){
    if(hit.collider.rigidbody2D !=null){
    hit.collider.rigidbody2D.velocity= newVector2(-10f,0f);

    }
    }
    }
    }
    }
     
  10. nikkouskouk

    nikkouskouk

    Joined:
    Jan 23, 2015
    Posts:
    29
    it is used for the direction of the raycast.Vector2.zero is the same with Vector2(0,0).Nothing special about it.
     
    sharpg likes this.
  11. sharpg

    sharpg

    Joined:
    Mar 10, 2015
    Posts:
    150
    Thanks :)
     
  12. sluice

    sluice

    Joined:
    Jan 31, 2014
    Posts:
    416
    @sharpg,

    next time, when you want to post code, put it in between these tag, without the spaces before and after the :
    [ CODE ] [ /CODE ]

    It will be much easier to read. :)
     
  13. sharpg

    sharpg

    Joined:
    Mar 10, 2015
    Posts:
    150
    I see, next time i must follow these instructions :)
    now i have another question.i am working on a simple baby game.I want to pluck thorn with a Thorn Plucker from my deer.I need when move my ThornPlucker with mouse drag so the thorn disappear.Actully in the game play thron plucker change to another thornplucker sprite which is holding thorn.So in game play user feel that thorn plucker actullay pluck the thorn.how can i do that?