- Home /
How to drag objects from UI into scen
Hi guys!
I'm trying to make a 2D game where you drag different characters from a UI at bottom of the screen and drop it in the scene. I just can't get it to work. This is what I've thought I'll do:
I have a row of buttons. When I click one of them a game object spawns at the position of the pointer (over the button, creating the illusion that you drag the game object out of the button). Then that game object is transferred to a OnMouseDrag() function and drags as usual wherever you want it.
I can make an object spawn at the button (if I use a worldspace canvas as a child of the camera). But the last part how I get the newly created object to stick to the mouse is where I'm lost.
What do you think. Is this the right way to go about it or should I do something else?
Greetings! I have just started learning Unity. I want to do an exact same thing in my project. Can you please tell me in short what you did from the start?? I will appreciate your help and reply.
Thanks in Advance!
Answer by LordCalvert1977 · Sep 20, 2017 at 01:25 PM
Maybe with a RaycastHit2D on some DragNDropManager object? Checking if the mouse hits something while the mouse is pressed. If it already does, ignore every other object. If it doesn't, pick up the new object. Then it's only an Update() on one object.
Ok this is what I did:
$$anonymous$$ake a world space canvas and put it as a child of the main camera.
$$anonymous$$ake a button with a simple Instantiate script on it.
$$anonymous$$ake an empty gameobject and put a script with this code in it.
Transform target; public Camera cam; // Because Camera.main is a expensive way to go about it
void Update() { //If the left mouse button is clicked. if (Input.Get$$anonymous$$ouseButton(0)) { //Get the mouse position on the screen and send a raycast into the game world from that position. Vector2 worldPoint = cam.ScreenToWorldPoint(Input.mousePosition); RaycastHit2D hit = Physics2D.Raycast(worldPoint, Vector2.zero); //If something was hit, the RaycastHit2D.collider will not be null and the the object must have the "$$anonymous$$onkey" tag and target has to be null if (hit.collider != null && hit.collider.tag == "$$anonymous$$onkey" && target == null) { target = hit.collider.transform; // Sets the target to be the transform that was hit } if (target != null) { target.position = worldPoint; // $$anonymous$$oves target with the mouse } } if (Input.Get$$anonymous$$ouseButtonUp(0)) { target = null; // Sets the target to null again } }
$$anonymous$$ake sure the objects you wanna drag has the right tag on them. In my case it's "$$anonymous$$onkey".
Profit! No but I think that was all I did.
$$anonymous$$aybe there are a more efficient way to go about it but this works.
Answer by tormentoarmagedoom · Sep 20, 2017 at 01:03 PM
Good day @LordCalvert1977 !
You need to use this 3 functions:
ScreenToWorldPoint, mousePosition, Instantiate
You need first to get a world corrds using Mouse position and ScreenToworldPoint function.
Vector3 MousePos = Camera.main.ScreenToWorldPoint(Input.mousePosition);
Once you have this, just need to Instantiate a GameObject at that position
Instantiate(GameObject prefab, Vector3 position, Quaternion rotation);
If helped you, mark the answer as good!
Bye :D
Thanks for the reply but it's not addressing my problem. I can drag an object already. And I can instantiate it by pressing an UI button. What I can't do is get the newly instantiated object to stick to the mouse. Or I can, but it's buggy and drops it sometimes. Sorry if I was unclear.
I need to do something like this, I think:
I press the ui button which instantiates the gameobject. Then while the mouse button still is pressed I need to trick the instantiated go that it's being dragged by the mouse. That's the step I'm missing.
And if I put a If statment in an Update method, checking if the mousebutton is pressed every object with that script comes flying. And also I'm trying to avoid Update as much as posslible.
Any thoughts?
Answer by rugvedkhandekar · Apr 10, 2018 at 07:51 PM
@LordCalvert1977 I partially understood what you did. Can you please explain me what do with the Empty game object with the Instantiate Script in it? And for instantiation, how does it take position of the mouse? Where did you put the script you have wrote above in your answer? what is 'cam' ? is 'cam' a variable? if yes, then which data type??
i'm trying to do a similar thing which you are trying to do, I would glad if you helped. Thanks in advance.