Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Drag drop game objects (without rigidbody) with the mouse

Discussion in 'Scripting' started by alph, Oct 18, 2010.

Thread Status:
Not open for further replies.
  1. alph

    alph

    Joined:
    Jul 20, 2010
    Posts:
    89
    I've searched and read, but haven't found any satisfactory answers..

    Lets say I have a clean project. I add a cube. When pressing play in Unity3D I want to move this cube with my mouse. Pseudo code:
    Code (csharp):
    1. function OnMouseDrag() {
    2.   // move gameObject along with the mouse
    3.   // transform = Mouse.transform ??
    4. }
    There are no rigid bodies involved, so I'm guessing this should be fairly simple without a lot of code?
     
    mekartikshah and Abdul20 like this.
  2. mole420

    mole420

    Joined:
    Oct 18, 2010
    Posts:
    21
    Try adding this to the object you want to drag

    Code (csharp):
    1. //Script to drag an object in world space using the mouse
    2.  
    3.  
    4. var screenSpace;
    5. var offset;
    6.  
    7. function OnMouseDown(){
    8.     //translate the cubes position from the world to Screen Point
    9.     screenSpace = Camera.main.WorldToScreenPoint(transform.position);
    10.      
    11.     //calculate any difference between the cubes world position and the mouses Screen position converted to a world point  
    12.     offset = transform.position - Camera.main.ScreenToWorldPoint(Vector3(Input.mousePosition.x,Input.mousePosition.y, screenSpace.z));
    13.    
    14. }
    15.  
    16. /*
    17. OnMouseDrag is called when the user has clicked on a GUIElement or Collider and is still holding down the mouse.
    18. OnMouseDrag is called every frame while the mouse is down.
    19. */
    20.  
    21. function OnMouseDrag () {
    22.  
    23.     //keep track of the mouse position
    24.     var curScreenSpace = Vector3(Input.mousePosition.x, Input.mousePosition.y, screenSpace.z);    
    25.  
    26.     //convert the screen mouse position to world point and adjust with offset
    27.     var curPosition = Camera.main.ScreenToWorldPoint(curScreenSpace) + offset;
    28.  
    29.     //update the position of the object in the world
    30.     transform.position = curPosition;
    31. }
     
    mekartikshah likes this.
  3. alph

    alph

    Joined:
    Jul 20, 2010
    Posts:
    89
    Perfect! Thanx :)
     
  4. YoungjaeKim

    YoungjaeKim

    Joined:
    Jul 18, 2013
    Posts:
    3
    For others who might get help like me, I changed code to C# version.
    (I'm Unity beginner so codes are verbose.)
    I tested it and it works.

    Code (csharp):
    1.  
    2. using UnityEngine;
    3. using System.Collections;
    4.  
    5. public class DragAndDrop : MonoBehaviour
    6. {  
    7.     private bool _mouseState;
    8.     private GameObject target;
    9.     public Vector3 screenSpace;
    10.     public Vector3 offset;
    11.  
    12.     // Use this for initialization
    13.     void Start ()
    14.     {
    15.    
    16.     }
    17.    
    18.     // Update is called once per frame
    19.     void Update ()
    20.     {
    21.         // Debug.Log(_mouseState);
    22.         if (Input.GetMouseButtonDown (0)) {
    23.  
    24.             RaycastHit hitInfo;
    25.             target = GetClickedObject (out hitInfo);
    26.             if (target != null) {
    27.                 _mouseState = true;
    28.                 screenSpace = Camera.main.WorldToScreenPoint (target.transform.position);
    29.                 offset = target.transform.position - Camera.main.ScreenToWorldPoint (new Vector3 (Input.mousePosition.x, Input.mousePosition.y, screenSpace.z));
    30.             }
    31.         }
    32.         if (Input.GetMouseButtonUp (0)) {
    33.             _mouseState = false;
    34.         }
    35.         if (_mouseState) {
    36.             //keep track of the mouse position
    37.             var curScreenSpace = new Vector3 (Input.mousePosition.x, Input.mousePosition.y, screenSpace.z);
    38.  
    39.             //convert the screen mouse position to world point and adjust with offset
    40.             var curPosition = Camera.main.ScreenToWorldPoint (curScreenSpace) + offset;
    41.  
    42.             //update the position of the object in the world
    43.             target.transform.position = curPosition;
    44.         }
    45.     }
    46.    
    47.    
    48.     GameObject GetClickedObject (out RaycastHit hit)
    49.     {
    50.         GameObject target = null;
    51.         Ray ray = Camera.main.ScreenPointToRay (Input.mousePosition);
    52.         if (Physics.Raycast (ray.origin, ray.direction * 10, out hit)) {
    53.             target = hit.collider.gameObject;
    54.         }
    55.  
    56.         return target;
    57.     }
    58. }
    59.  
     
    Last edited: Jul 18, 2013
  5. Smus

    Smus

    Joined:
    Dec 16, 2012
    Posts:
    1
    Does that work for 2D aswell?
     
  6. st-greenman

    st-greenman

    Joined:
    Jan 17, 2015
    Posts:
    1
    Thank you so much mole420 and YoungjaeKim for that code. Works like a charm.
     
    Abdul20 likes this.
  7. Cellet

    Cellet

    Joined:
    Sep 23, 2014
    Posts:
    1
    Hey I think I found a better solution at least for 2D! :)
    Code (CSharp):
    1. void OnMouseDrag(){
    2.        
    3.             Vector3 mousePos = Camera.main.ScreenToWorldPoint(Input.mousePosition);
    4.             mousePos.z = 0f;
    5.             transform.position = mousePos;
    6.        
    7.     }
     
    Last edited: Jul 29, 2015
    Nyan_Cat123 and entoon like this.
  8. DonLoquacious

    DonLoquacious

    Joined:
    Feb 24, 2013
    Posts:
    1,667
    No need to call z = 0, because Input.mousePosition is a Vector2 being used to make a Vector3, so z is going to be zero regardless. Also, this is the exact same thing posted as one of the first responses, except you use Input.mousePosition directly for ScreenToWorldPoint instead of converting it to a Vector3 first (ScreenToWorldPoint at the time that the original post was made might not have accepted Vector2s as a parameter, maybe).

    Also, please don't necro threads without a good reason.
     
  9. sudam

    sudam

    Joined:
    Jul 22, 2016
    Posts:
    2
    I want drag game object only in fixed path.so any one can help me.......
     
  10. kaiguy720

    kaiguy720

    Joined:
    Jan 25, 2016
    Posts:
    2
    have you tried just setting one of the values for a move axes to 0
     
  11. kaiguy720

    kaiguy720

    Joined:
    Jan 25, 2016
    Posts:
    2
    u prolly dont care anymore tho so nvm
     
  12. fascinator

    fascinator

    Joined:
    Apr 28, 2014
    Posts:
    2
    You are awesome YoungjaeKim, that script rocks! Thank you very much!
     
  13. abdougamer0107

    abdougamer0107

    Joined:
    Jan 23, 2022
    Posts:
    1
    do you have it with rigid body
     
  14. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    11,321
    Please look at the dates of posts before replying to them.

    Let's close this necro magnet of a thread. :)
     
    rpgcubed and Kurt-Dekker like this.
Thread Status:
Not open for further replies.