Game Development Stack Exchange is a question and answer site for professional and independent game developers. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I just wanna ask if it is possible to drag and drop a gameobject in specific place. For example i want to put an LED sprite in the hole of the breadboard sprite.

share
    
Have you tried it? – Tyyppi_77 2 hours ago
    
Im confused by what you mean. Do you mean in the editor, itself? And when you say "specific place" do you mean absolutely specific? That is to say, being able to specifically drop an object at (for example) the position (10, 15, 50), and not roughly the co-ordinates, like say (10, 15, 50.5)? – Timelord64 1 hour ago

I', going to share my drang n drop code and you can work it out from there. Here it is :

    using UnityEngine;
    using UnityEngine.EventSystems;
    public class DropZone : MonoBehaviour, IDropHandler 
    {
      public void OnDrop(PointerEventData eventData) 
      {
          Debug.Log (eventData.pointerDrag.name + " was dropped on " + gameObject.name);
          //Do checks for what is being drop here 
      }
    }

Then for the actual item we want to drag we use this:

using UnityEngine;
using UnityEngine.UI;
using UnityEngine.EventSystems;

public class Draggable : MonoBehaviour,
          IBeginDragHandler, IDragHandler, IEndDragHandler {

    public void OnBeginDrag(PointerEventData eventData) {}

    public void OnDrag(PointerEventData eventData) {
        //Debug.Log ("OnDrag");
        transform.position = eventData.position;
        }

    public void OnEndDrag(PointerEventData eventData) {}
}

For more infromation,solutions and a in-depth look into this problem you can refer to this question on stackoverflow.

share|improve this answer

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.