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 do you drop and drop a 2D object in unity 5?

Discussion in 'Getting Started' started by Drowzee, Jul 29, 2017.

  1. Drowzee

    Drowzee

    Joined:
    Aug 16, 2015
    Posts:
    27
    I'm trying to make it so I can click on objects falling from the top of the screen, and drop them to a certain point, then letting go so it continues falling.

    However I'm really new to unity and can't find and recent answers, I was wondering if someone could lend a helping hand ^^ I'm not gonna ask for exact code cuz then I might not learn anything, but if someone knows the functions I might need and maybe an idea of just how it should be implemented I'd appreciate the help :)
     
  2. orb

    orb

    Joined:
    Nov 24, 2010
    Posts:
    3,033
    Put a collider on each object you want to make clickable. Set a bool for drag in OnMouseDown(), move it if dragging in Update() and toggle the flag back in OnMouseUp(). That's the gist of it anyway :)

    Another way of doing it is to move clicking and handling into a generic manager script instead of on each project. This can be useful for multiple pickups.
     
    Kiwasi likes this.
  3. Drowzee

    Drowzee

    Joined:
    Aug 16, 2015
    Posts:
    27
    I'll give this a go, thanks :)
     
  4. Drowzee

    Drowzee

    Joined:
    Aug 16, 2015
    Posts:
    27
    Ok so its kind of working, but when I drag the object, in looks like im making a new one instead of moving it, almost like im making a trail of this object behind my mouse
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class DragandDrop : MonoBehaviour
    6. {
    7.  
    8.     private bool isDragging;
    9.     private Vector3 targetPos;
    10.  
    11.     // Use this for initialization
    12.     void Start ()
    13.     {
    14.         isDragging = false;
    15.     }
    16.  
    17.     // Update is called once per frame
    18.     void Update ()
    19.     {
    20.         if(isDragging == true)
    21.         {
    22.             targetPos = Camera.main.ScreenToWorldPoint(Input.mousePosition);
    23.             targetPos.z = transform.position.z;
    24.             Instantiate(this, targetPos, Quaternion.identity);
    25.         }
    26.     }
    27.  
    28.     void OnMouseDown()
    29.     {
    30.         isDragging = true;
    31.     }
    32.  
    33.     void OnMouseUp()
    34.     {
    35.         isDragging = false;
    36.     }
    37. }
    not quite sure whats up.

    Taking a second look, it's probably because I used instantiate, thats basically creating right? Not sure what to use instead though
     
  5. orb

    orb

    Joined:
    Nov 24, 2010
    Posts:
    3,033
    Yeah, you're instantiating a new one every Update(), which is not a great idea in the long run. Or even for two frames ;)

    Just set the transform.position to the mouse position. You could also set the Z position to somewhere nearer the camera.
     
    Kiwasi likes this.
  6. Drowzee

    Drowzee

    Joined:
    Aug 16, 2015
    Posts:
    27
    Tried XD
    Code (CSharp):
    1. if(isDragging == true)
    2.         {
    3.             targetPos = Camera.main.ScreenToWorldPoint(Input.mousePosition);
    4.             targetPos.x = transform.position.x;
    5.             targetPos.y = transform.position.y;
    6.             this.transform.position = new Vector2(targetPos.x, targetPos.y);
    7.         }
     
  7. orb

    orb

    Joined:
    Nov 24, 2010
    Posts:
    3,033
    You're setting the x/y positions of the object to the x/y positions of the object. Remove lines 4 and 5.
     
    Drowzee likes this.
  8. Drowzee

    Drowzee

    Joined:
    Aug 16, 2015
    Posts:
    27
    :eek: progress! :D
     
    JoeStrout likes this.
  9. Kiwasi

    Kiwasi

    Joined:
    Dec 5, 2013
    Posts:
    16,860
    Later on this is going to do weird things with clicks on the UI. My preference is to use the event system interfaces. It's slightly more boilerplate code, but it's worth playing nice with the UI.

    You will need to add a Physics2DRaycaster to the camera.