How to drag and drop 2D UI image on 3D Game object and then instantiate 3D game object at the position where...












0














I have a cube in my scene so what I want is to drag and drop a 2d UI image of wrench tool over the cube and as soon as I drop that image on 3d cube the wrench prefab must be instantiated there.



Here is an image showing what I need



drag and drop on cube



I am using below code to drag my UI Image around the scene but dont know how to drop this on 3D Cube and instantiate a wrench prefab



using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class wrench : MonoBehaviour {


public bool Dragging = false;
public bool collision = false;
Vector3 position;

// Use this for initialization
void Start () {

}

// Update is called once per frame
void Update () {

}

public void BeginDrag(){
position = gameObject.transform.position;
Dragging = true;
}

public void Drag(){
transform.position = Input.mousePosition;
}

public void Drop(){
if (!collision) {
gameObject.transform.position = position;
}
Dragging = false;
}
}









share|improve this question





























    0














    I have a cube in my scene so what I want is to drag and drop a 2d UI image of wrench tool over the cube and as soon as I drop that image on 3d cube the wrench prefab must be instantiated there.



    Here is an image showing what I need



    drag and drop on cube



    I am using below code to drag my UI Image around the scene but dont know how to drop this on 3D Cube and instantiate a wrench prefab



    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;

    public class wrench : MonoBehaviour {


    public bool Dragging = false;
    public bool collision = false;
    Vector3 position;

    // Use this for initialization
    void Start () {

    }

    // Update is called once per frame
    void Update () {

    }

    public void BeginDrag(){
    position = gameObject.transform.position;
    Dragging = true;
    }

    public void Drag(){
    transform.position = Input.mousePosition;
    }

    public void Drop(){
    if (!collision) {
    gameObject.transform.position = position;
    }
    Dragging = false;
    }
    }









    share|improve this question



























      0












      0








      0







      I have a cube in my scene so what I want is to drag and drop a 2d UI image of wrench tool over the cube and as soon as I drop that image on 3d cube the wrench prefab must be instantiated there.



      Here is an image showing what I need



      drag and drop on cube



      I am using below code to drag my UI Image around the scene but dont know how to drop this on 3D Cube and instantiate a wrench prefab



      using System.Collections;
      using System.Collections.Generic;
      using UnityEngine;

      public class wrench : MonoBehaviour {


      public bool Dragging = false;
      public bool collision = false;
      Vector3 position;

      // Use this for initialization
      void Start () {

      }

      // Update is called once per frame
      void Update () {

      }

      public void BeginDrag(){
      position = gameObject.transform.position;
      Dragging = true;
      }

      public void Drag(){
      transform.position = Input.mousePosition;
      }

      public void Drop(){
      if (!collision) {
      gameObject.transform.position = position;
      }
      Dragging = false;
      }
      }









      share|improve this question















      I have a cube in my scene so what I want is to drag and drop a 2d UI image of wrench tool over the cube and as soon as I drop that image on 3d cube the wrench prefab must be instantiated there.



      Here is an image showing what I need



      drag and drop on cube



      I am using below code to drag my UI Image around the scene but dont know how to drop this on 3D Cube and instantiate a wrench prefab



      using System.Collections;
      using System.Collections.Generic;
      using UnityEngine;

      public class wrench : MonoBehaviour {


      public bool Dragging = false;
      public bool collision = false;
      Vector3 position;

      // Use this for initialization
      void Start () {

      }

      // Update is called once per frame
      void Update () {

      }

      public void BeginDrag(){
      position = gameObject.transform.position;
      Dragging = true;
      }

      public void Drag(){
      transform.position = Input.mousePosition;
      }

      public void Drop(){
      if (!collision) {
      gameObject.transform.position = position;
      }
      Dragging = false;
      }
      }






      user-interface unity3d drag-and-drop






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 20 at 10:59









      Basile Perrenoud

      3,12911737




      3,12911737










      asked Nov 20 at 6:26









      Nouman Khan

      116




      116
























          1 Answer
          1






          active

          oldest

          votes


















          0














          usually on stack overflow you should add some information about what you did and show some code so we can help figure out what is the problem in your approach. People won't write all the code for you without knowing what your project currently look like.



          That being said, if you don't have anything to show yet and just wonder where to start, here are an idea:



          You can get the position of the mouse in the world, and shoot a ray from it. Then check if this ray hits and object. like this:



          if (Input.GetMouseButtonUp(0))
          {
          ray = Camera.main.ScreenPointToRay(Input.mousePosition);

          if (Physics.Raycast(ray, out hit))
          {
          Debug.Log(hit); // Find a way to figure out what object you hit
          }
          }





          share|improve this answer





















          • i.stack.imgur.com/fQVS9.jpg hey man please check this picture this is what i want
            – Nouman Khan
            Nov 20 at 6:55










          • I know what you want, what did you code to try and do it? Can you do it with the idea I gave in my answer? You need to try coding something
            – Basile Perrenoud
            Nov 20 at 6:59












          • i simple written a code which is dragging this UI Image around the 3d scene now what i want is that how to drop that image on that cube and instantiate a wrench prefab let me show u the code of dragging
            – Nouman Khan
            Nov 20 at 7:03










          • please check my question i have added a code i use this code to dragging this ui image around the scene
            – Nouman Khan
            Nov 20 at 7:04










          • Do you understand the code I put in my answer? Just use something like it in your drop function. To create the prefab, use Instantiate.
            – Basile Perrenoud
            Nov 20 at 11:04











          Your Answer






          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "1"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53387398%2fhow-to-drag-and-drop-2d-ui-image-on-3d-game-object-and-then-instantiate-3d-game%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          0














          usually on stack overflow you should add some information about what you did and show some code so we can help figure out what is the problem in your approach. People won't write all the code for you without knowing what your project currently look like.



          That being said, if you don't have anything to show yet and just wonder where to start, here are an idea:



          You can get the position of the mouse in the world, and shoot a ray from it. Then check if this ray hits and object. like this:



          if (Input.GetMouseButtonUp(0))
          {
          ray = Camera.main.ScreenPointToRay(Input.mousePosition);

          if (Physics.Raycast(ray, out hit))
          {
          Debug.Log(hit); // Find a way to figure out what object you hit
          }
          }





          share|improve this answer





















          • i.stack.imgur.com/fQVS9.jpg hey man please check this picture this is what i want
            – Nouman Khan
            Nov 20 at 6:55










          • I know what you want, what did you code to try and do it? Can you do it with the idea I gave in my answer? You need to try coding something
            – Basile Perrenoud
            Nov 20 at 6:59












          • i simple written a code which is dragging this UI Image around the 3d scene now what i want is that how to drop that image on that cube and instantiate a wrench prefab let me show u the code of dragging
            – Nouman Khan
            Nov 20 at 7:03










          • please check my question i have added a code i use this code to dragging this ui image around the scene
            – Nouman Khan
            Nov 20 at 7:04










          • Do you understand the code I put in my answer? Just use something like it in your drop function. To create the prefab, use Instantiate.
            – Basile Perrenoud
            Nov 20 at 11:04
















          0














          usually on stack overflow you should add some information about what you did and show some code so we can help figure out what is the problem in your approach. People won't write all the code for you without knowing what your project currently look like.



          That being said, if you don't have anything to show yet and just wonder where to start, here are an idea:



          You can get the position of the mouse in the world, and shoot a ray from it. Then check if this ray hits and object. like this:



          if (Input.GetMouseButtonUp(0))
          {
          ray = Camera.main.ScreenPointToRay(Input.mousePosition);

          if (Physics.Raycast(ray, out hit))
          {
          Debug.Log(hit); // Find a way to figure out what object you hit
          }
          }





          share|improve this answer





















          • i.stack.imgur.com/fQVS9.jpg hey man please check this picture this is what i want
            – Nouman Khan
            Nov 20 at 6:55










          • I know what you want, what did you code to try and do it? Can you do it with the idea I gave in my answer? You need to try coding something
            – Basile Perrenoud
            Nov 20 at 6:59












          • i simple written a code which is dragging this UI Image around the 3d scene now what i want is that how to drop that image on that cube and instantiate a wrench prefab let me show u the code of dragging
            – Nouman Khan
            Nov 20 at 7:03










          • please check my question i have added a code i use this code to dragging this ui image around the scene
            – Nouman Khan
            Nov 20 at 7:04










          • Do you understand the code I put in my answer? Just use something like it in your drop function. To create the prefab, use Instantiate.
            – Basile Perrenoud
            Nov 20 at 11:04














          0












          0








          0






          usually on stack overflow you should add some information about what you did and show some code so we can help figure out what is the problem in your approach. People won't write all the code for you without knowing what your project currently look like.



          That being said, if you don't have anything to show yet and just wonder where to start, here are an idea:



          You can get the position of the mouse in the world, and shoot a ray from it. Then check if this ray hits and object. like this:



          if (Input.GetMouseButtonUp(0))
          {
          ray = Camera.main.ScreenPointToRay(Input.mousePosition);

          if (Physics.Raycast(ray, out hit))
          {
          Debug.Log(hit); // Find a way to figure out what object you hit
          }
          }





          share|improve this answer












          usually on stack overflow you should add some information about what you did and show some code so we can help figure out what is the problem in your approach. People won't write all the code for you without knowing what your project currently look like.



          That being said, if you don't have anything to show yet and just wonder where to start, here are an idea:



          You can get the position of the mouse in the world, and shoot a ray from it. Then check if this ray hits and object. like this:



          if (Input.GetMouseButtonUp(0))
          {
          ray = Camera.main.ScreenPointToRay(Input.mousePosition);

          if (Physics.Raycast(ray, out hit))
          {
          Debug.Log(hit); // Find a way to figure out what object you hit
          }
          }






          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 20 at 6:38









          Basile Perrenoud

          3,12911737




          3,12911737












          • i.stack.imgur.com/fQVS9.jpg hey man please check this picture this is what i want
            – Nouman Khan
            Nov 20 at 6:55










          • I know what you want, what did you code to try and do it? Can you do it with the idea I gave in my answer? You need to try coding something
            – Basile Perrenoud
            Nov 20 at 6:59












          • i simple written a code which is dragging this UI Image around the 3d scene now what i want is that how to drop that image on that cube and instantiate a wrench prefab let me show u the code of dragging
            – Nouman Khan
            Nov 20 at 7:03










          • please check my question i have added a code i use this code to dragging this ui image around the scene
            – Nouman Khan
            Nov 20 at 7:04










          • Do you understand the code I put in my answer? Just use something like it in your drop function. To create the prefab, use Instantiate.
            – Basile Perrenoud
            Nov 20 at 11:04


















          • i.stack.imgur.com/fQVS9.jpg hey man please check this picture this is what i want
            – Nouman Khan
            Nov 20 at 6:55










          • I know what you want, what did you code to try and do it? Can you do it with the idea I gave in my answer? You need to try coding something
            – Basile Perrenoud
            Nov 20 at 6:59












          • i simple written a code which is dragging this UI Image around the 3d scene now what i want is that how to drop that image on that cube and instantiate a wrench prefab let me show u the code of dragging
            – Nouman Khan
            Nov 20 at 7:03










          • please check my question i have added a code i use this code to dragging this ui image around the scene
            – Nouman Khan
            Nov 20 at 7:04










          • Do you understand the code I put in my answer? Just use something like it in your drop function. To create the prefab, use Instantiate.
            – Basile Perrenoud
            Nov 20 at 11:04
















          i.stack.imgur.com/fQVS9.jpg hey man please check this picture this is what i want
          – Nouman Khan
          Nov 20 at 6:55




          i.stack.imgur.com/fQVS9.jpg hey man please check this picture this is what i want
          – Nouman Khan
          Nov 20 at 6:55












          I know what you want, what did you code to try and do it? Can you do it with the idea I gave in my answer? You need to try coding something
          – Basile Perrenoud
          Nov 20 at 6:59






          I know what you want, what did you code to try and do it? Can you do it with the idea I gave in my answer? You need to try coding something
          – Basile Perrenoud
          Nov 20 at 6:59














          i simple written a code which is dragging this UI Image around the 3d scene now what i want is that how to drop that image on that cube and instantiate a wrench prefab let me show u the code of dragging
          – Nouman Khan
          Nov 20 at 7:03




          i simple written a code which is dragging this UI Image around the 3d scene now what i want is that how to drop that image on that cube and instantiate a wrench prefab let me show u the code of dragging
          – Nouman Khan
          Nov 20 at 7:03












          please check my question i have added a code i use this code to dragging this ui image around the scene
          – Nouman Khan
          Nov 20 at 7:04




          please check my question i have added a code i use this code to dragging this ui image around the scene
          – Nouman Khan
          Nov 20 at 7:04












          Do you understand the code I put in my answer? Just use something like it in your drop function. To create the prefab, use Instantiate.
          – Basile Perrenoud
          Nov 20 at 11:04




          Do you understand the code I put in my answer? Just use something like it in your drop function. To create the prefab, use Instantiate.
          – Basile Perrenoud
          Nov 20 at 11:04


















          draft saved

          draft discarded




















































          Thanks for contributing an answer to Stack Overflow!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.





          Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


          Please pay close attention to the following guidance:


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53387398%2fhow-to-drag-and-drop-2d-ui-image-on-3d-game-object-and-then-instantiate-3d-game%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          "Incorrect syntax near the keyword 'ON'. (on update cascade, on delete cascade,)

          Alcedinidae

          Origin of the phrase “under your belt”?