How to make a drag and drop game in Unity

I'll show you how to quickly get your drag and drop game up and running while teaching you a few key Unity concepts along the way, such as:
Drag & drop
Detecting proximity (distance between objects)
Playing sounds
=========
❤️ Become a Tarobro on Patreon: / tarodev
=========
🔔 SUBSCRIBE: bit.ly/3eqG1Z6
🗨️ DISCORD: / discord
✅ MORE TUTORIALS: / tarodev
0:00 Intro
0:11 Creating the prefabs
1:05 Drag and drop the puzzle piece
7:28 Setting up the slot
9:14 PuzzleManager (spawning pieces)
11:45 Selecting random list elements (puzzle pieces)
16:45 Dropping the pieces into the slot

Пікірлер: 91

  • @daniellaplanagimeno3927
    @daniellaplanagimeno39277 ай бұрын

    A good note for everyone, if you are trying to use this scripts for moving objects in a canvas, use OnPointerDown instead of OnMouseDown because canvas is a different element that in Unity terms is not inside screen so detecting mouse position in canvas will get confused

  • @jadonmullinex9389
    @jadonmullinex93892 жыл бұрын

    I recently found your channel... you are my new favorite Unity Dev channel! I'm working on a Mobile Kids Game and this is perfect! Love your content!

  • @rhalferty
    @rhalferty Жыл бұрын

    Love your videos. So simple, but so helpful. They nail down the basics with easy to follow examples.

  • @fuzeworksit6465
    @fuzeworksit64652 жыл бұрын

    I don't usually comment but this was great man. I work as an IT Manager for a school and I'm learning to use Unity to build some games for the kids. This was super helpful. Keep it up please! 💪💪

  • @vietanhnguyen1415
    @vietanhnguyen14152 жыл бұрын

    Thanks man, your video is my lifesaver

  • @alanadonascimentofreitas4904
    @alanadonascimentofreitas49043 жыл бұрын

    Thank you! Perfect clip!!! Awesome! AMAZING!

  • @amelialewey9221
    @amelialewey9221 Жыл бұрын

    This helped me start on a food assembling game! Thank you for uploading this video!

  • @Tarodev

    @Tarodev

    Жыл бұрын

    Have fun 😊

  • @bluepenguin_01
    @bluepenguin_01 Жыл бұрын

    great tutorial, thanks man

  • @hdggfhjb6768
    @hdggfhjb67683 жыл бұрын

    Your work is great

  • @fitriyansyah_
    @fitriyansyah_ Жыл бұрын

    thank you so much for such wonderful and easy to follow tutorial.. waiting to see another kids game tutorial, maybe coloring games?

  • @USSR-Lenin-Stalin-Forever
    @USSR-Lenin-Stalin-Forever9 ай бұрын

    Thanks for the lesson, the only remark is that in the middle of the lesson we started making Spawn Manager without completing the implementation of drop, because of this it was hard to go down from one implementation to another. I think it was necessary to do Drag and Drop first and then show how to implement it through Spawn Manager.

  • @keziavb
    @keziavb3 жыл бұрын

    hi, this is a great tutorial, thank you so much! you're awesome

  • @Tarodev

    @Tarodev

    3 жыл бұрын

    Glad I could teach you something :)

  • @ibrahemslman8159
    @ibrahemslman81592 жыл бұрын

    you are my hero

  • @mactram7293
    @mactram72934 ай бұрын

    tk very much, it help me so much

  • @noobcraft5712
    @noobcraft57123 жыл бұрын

    Now I know how to add sounds to my game. :)

  • @micaiahstevens8840

    @micaiahstevens8840

    2 жыл бұрын

    I would follow up with his longer explanation of using a AudioManager, such as the Structured video. This gives you more flexibility and can adding settings later. You can also spawn an audio prefab, and initialize it, with your sound as an alternative.

  • @noobcraft5712

    @noobcraft5712

    2 жыл бұрын

    @@micaiahstevens8840 thanks

  • @Aurillia
    @Aurillia2 жыл бұрын

    Tarodev: "Hopefully this helped you on your way to making whatever game it is, probably a kiddy game..." Meanwhile, here's me using what I learned from this video to swap weapons and ammo types around in my endless shooter game 👀

  • @Tarodev

    @Tarodev

    2 жыл бұрын

    You're abusing the power!!

  • @Ottiya
    @Ottiya2 жыл бұрын

    Thank you so much for this! This is awesome :) If you'd be so kind to help: How would you spawn just one set of slot + piece (e.g. the #1 slot and #1 piece) instead of a random number from a list?

  • @samilaferreira5658
    @samilaferreira56582 жыл бұрын

    Thanks for your time and for this tutorial. Very handy. I'm facing some problems with the puzzle pieces since they don't move. I can listen the sound but the pieces are stuck. I have reload many times this video but could not figure it out.

  • @jonecabelchev3719
    @jonecabelchev3719 Жыл бұрын

    Great videos! I learn a lot about unity from you. Appreciate it, please keep up the good work 🙇‍♂ I have one question though and this is kind of confusing me at the moment... I have found two approaches to implement drag and drop. One is to use sprites with box collider. And the other one is images with event system. What is the correct way to implement drag and drop? Or maybe in which case which approach to use? For example, you implement the drag and drop directly on sprites with box collider. But I couldn't make this to work on images. So another approach - drag and drop for images using the Event System and implement the appropriate handlers. Please advise. Thank you!!!

  • @johnangelomanuel1967
    @johnangelomanuel19672 жыл бұрын

    Hi, this is awesome! Liked and Subscribed. Is there a way to show victory panel when they placed all of them in the slot correctly. Thank you in advance.

  • @nehal_chandra2777
    @nehal_chandra27772 жыл бұрын

    my brain is too dirty for this

  • @micaiahstevens8840

    @micaiahstevens8840

    2 жыл бұрын

    I recently found Tarodev and love his content. I am HOPING its an accent, my wife was like he is NOT saying what I think he is saying. I can't even think how to say 'Slot' correctly. Its like Slaat, without the 'u' sound.

  • @spekticat

    @spekticat

    Ай бұрын

    What did he say about my parents

  • @shuganyaravi6765
    @shuganyaravi6765 Жыл бұрын

    Hi thank you so much for the video...how do I rearrange the puzzle piece

  • @marialuongo9501
    @marialuongo9501 Жыл бұрын

    Hi, thank you so much for this! But i have a question. how do I only spawn a new puzzle piece after it has collided with its puzzle slot?

  • @MstrDennis777
    @MstrDennis7772 жыл бұрын

    That's great, thank you!!!! But how to finish this game, I mean It would be nice to see the next level (scene) or a win panel after we fullfill empy slots. How to do this?

  • @pramothr301
    @pramothr301 Жыл бұрын

    Hello Tarodev, Thank you very much for your video, it was a good example for my project. I am developing 2D game, implementing the same concept drag and drop. I wanted to know how to re-arrange the objects(i.e., puzzle pieces). Can you please help with this..

  • @JudasBricott
    @JudasBricott2 жыл бұрын

    Hello, great tutorial thank you I did not know it could be so easy. Do you know if it is easily feasible in unity to cut a "cake" ( a disk ) into several parts ( quarters for example ) ? And then drag and drop those parts ?

  • @micaiahstevens8840

    @micaiahstevens8840

    2 жыл бұрын

    That sounds exactly like a 'Radial Menu', and then you apply your scripts and logic along the same lines as this video.

  • @katilehtonen614
    @katilehtonen614 Жыл бұрын

    this is a great tutorial, but I was just wondering how could I make a good ending for a game like this? Like how to maybe restart or make something nice happen on a screen after every piece have been moved to their right slots? And I am still a beginner so I am not sure in what script something like that needs to be written? Maybe the manager?

  • @michael.corlenome
    @michael.corlenome2 жыл бұрын

    How can I drop the object to where my mouse dropped it instead of where it originally started

  • @datd4063
    @datd40633 жыл бұрын

    Hello Tarodev, thanks for your tutorial. I wanted to know whether you can make a tutorial where you can drag and drop the items. In Addition the item should rotate with a mouse movement. Thank you in advanced.

  • @Tarodev

    @Tarodev

    3 жыл бұрын

    Do you mean adding a bit of swing to it as you move it left and right? You could compare current position with the position n seconds ago and lerp rotation based on the delta. If you mean rotating the more you slide it across the screen you could just add rotation based on the x position. Or did I miss your objective completely?

  • @datd4063

    @datd4063

    3 жыл бұрын

    @@Tarodev Hello Tarodev, thanks for your reply. I meant for example: Rotate the figure 6 to the figure 9 with a 180 degree mouse movement. I tried to recreate a tangram game, where you have to move several pieces and rotate it to complete a big geometrical shape.

  • @datd4063

    @datd4063

    3 жыл бұрын

    @@Tarodev A example can be found on this website: mathigon.org/tangram

  • @Tarodev

    @Tarodev

    3 жыл бұрын

    @@datd4063 Ah! So to achieve that, each shape should have an object as a child which will contain the outline and the handle which pops up. When the player clicks the shape you enable the child object. Once they grab the rotation handle it should be as simple as rotating the shape on its pivot... If you put the rotation handle on the up axis you could simply do something like: transform.up = Camera.main.ScreenToWorldPoint(Input.mousePosition); Come into my discord if you need additional clarification :)

  • @datd4063

    @datd4063

    3 жыл бұрын

    @@Tarodev Thank you. I will try it. I also joined your discord server.

  • @andubunea9627
    @andubunea96273 жыл бұрын

    Can you make a video on how you made such good graphics in unity 3d ?

  • @Tarodev

    @Tarodev

    3 жыл бұрын

    I sure can! Stay tuned :)

  • @andubunea9627

    @andubunea9627

    3 жыл бұрын

    @@Tarodev very cool

  • @andubunea9627

    @andubunea9627

    3 жыл бұрын

    @@Tarodev OH just a recomandation you can make a discord server for subscribers to talk hang out give video ideas and stuff

  • @jaybob19904
    @jaybob19904 Жыл бұрын

    I am having an issue I followed along but when I go to grab the piece it sets the transform to that of the camera and there was a point when it didn't move at all but was reacting to the OnMouseDown

  • @user-rj4ef8dv9o
    @user-rj4ef8dv9o10 ай бұрын

    where i can get those auido source?

  • @wasab1z
    @wasab1z2 жыл бұрын

    What do you have in your Main Camera folder? How did you make your Scene the same background as your Game

  • @micaiahstevens8840

    @micaiahstevens8840

    2 жыл бұрын

    I took a look and it was HARD to tell, he could have done it multiple ways. A Camera with the settings to SolidColor (then choose a Green Color) A Canvas with an Image. A GameObject with a SpriteRenderer (More in line with how he coded the rest of the game) Either way you need some Image, or SpriteRenderer to set the sprite to a graphics, I like png's Looking at the video again, it seems more solid color, but all three are options. Than size, place according to your game.

  • @dragonarc_gaming
    @dragonarc_gaming Жыл бұрын

    15:19 can you tell me what does this do? -> spawnedPiece.Init(spawnedSlot); I don't really understand how do you spawn the same sprite on both puzzle piece and slot piece.

  • @Agentti_Kowalski
    @Agentti_Kowalski Жыл бұрын

    my transfrom.postion is not moving the character but i can hear the drop and pickup sounds

  • @ShyDeaw
    @ShyDeaw3 жыл бұрын

    Sir, can we random the place of object? Thank you so much

  • @micaiahstevens8840

    @micaiahstevens8840

    2 жыл бұрын

    Will include both replies to address random, see other commenter transform.position = Random.insideUnitSphere * 5; (This will need to worked around a bit if your in UI or Scene view placement of your game.) This DOES run into overlap, but that's a BIT more complicated.

  • @navodyaliyanage6297
    @navodyaliyanage62972 жыл бұрын

    can u plz share the github link as well?

  • @doctortruth1179
    @doctortruth117911 ай бұрын

    Can I put that on canvas?

  • @mertarslang
    @mertarslang Жыл бұрын

    Hello, First of all, thank you very much for your video, it was a good example for my project assignment, but I have a question. I'm developing a 3D game and I want to implement this puzzle you made there. When I brought it directly, I noticed that there were problems with the dragging. For this reason, I want this puzzle game to appear as a pop-up when I create a UI screen and click on any object in the game. Is it possible? I hope you will answer, I wish all the best!

  • @mertarslang

    @mertarslang

    Жыл бұрын

    I guess, "How can I do it on Canvas?" I'm talking about this.

  • @georgebowen2555
    @georgebowen2555 Жыл бұрын

    I have an issue where the Pieces and slots are spawning in but arent showing on the camera?

  • @jayskylark8692
    @jayskylark86922 ай бұрын

    How did you export it to a phone or a table?

  • @rubenvictorcallacondobeliz7135
    @rubenvictorcallacondobeliz713518 күн бұрын

    I have a question, why when you pull the image to the work screen does it look like SPRITERENDERER? Because when you pull an image it is only set as an image, is it possible that you have your Unity development environment configured differently? Or is it different in the new versions?

  • @bharatgupta1937
    @bharatgupta1937 Жыл бұрын

    Guys can someone help me with the sprites folder?

  • @user-rj4ef8dv9o
    @user-rj4ef8dv9o6 ай бұрын

    can u make a Visual Scripting verson of this ?

  • @mousamohamed5789
    @mousamohamed57892 жыл бұрын

    i have no function order by for slotsPrefabsSlots List ?

  • @micaiahstevens8840

    @micaiahstevens8840

    2 жыл бұрын

    This is because you need to include.. 'Using System.Linq' At the top of your files with the other using.

  • @choiscoc_tv1000
    @choiscoc_tv1000 Жыл бұрын

    i try to copy what you do sir but nothing happen. can you guide me why sir?

  • @avia4281
    @avia42817 ай бұрын

    Can you please make a simple felt game for children. Example 1 tree and 2 apples able to group and shake tree with apples and after remove one apple and shake tree group with 1 apple then no apples. Apples can lie on floor.

  • @lirikshack7672
    @lirikshack76728 ай бұрын

    urgent help, how do I add score to this script?

  • @Zack01OFFICIAL
    @Zack01OFFICIAL2 жыл бұрын

    Can you share the scripts......Its hard to see in this video because of my slow internet connection.

  • @muhammadzahirbinsubakhir7995
    @muhammadzahirbinsubakhir79952 жыл бұрын

    how can i set the position of the the dragndrop button ? not to random place the position

  • @micaiahstevens8840

    @micaiahstevens8840

    2 жыл бұрын

    Will include both replies to address random, see other commentor Vector3 pos; One way is to set the pos.x = Rnadom.Range(-25, 25) myObject.transform.position = pos; (Since you can't change position directly) and do that for each x/y/z you want, depending on how you see your scene up. This DOES run into overlap, but that's a BIT more complicated.

  • @yannickgervais240
    @yannickgervais240 Жыл бұрын

    I did the same thing you did and it do not drag. The sound plays as it clicked and supposedly dropped but the sprite do not move.

  • @yannickgervais240

    @yannickgervais240

    Жыл бұрын

    ok, I usually use 3D project and I needed to use drag in a 3d project. This code will not work if you mode is in 3d if you do not set you main cam in orthographic mode.

  • @spekticat
    @spekticatАй бұрын

    Oh he said SLOT

  • @nikomationsproblems
    @nikomationsproblems Жыл бұрын

    Can You Add Numbers Game To Google Play?

  • @Tarodev

    @Tarodev

    Жыл бұрын

    Of course you can 😊

  • @nikomationsproblems

    @nikomationsproblems

    Жыл бұрын

    Link?

  • @nikomationsproblems

    @nikomationsproblems

    3 ай бұрын

    Add Multiple Levels

  • @humayrabakar4207
    @humayrabakar42072 жыл бұрын

    I cant drag object. Object not set to an instance

  • @micaiahstevens8840

    @micaiahstevens8840

    2 жыл бұрын

    not set to an instance, was discussed how to figure this out in another video. Basically Unity is saying you did not setup your object in the editor, for prefabs and sprite renderers and such.

  • @technofeeliak
    @technofeeliak Жыл бұрын

    What do you keep saying "slotch"? What's a "slotch"?

  • @DannyBoy443
    @DannyBoy443 Жыл бұрын

    I'm not complaining, but again, can someone tell me why people feel the need to put themselves on camera during these tutorials? No offense but we don't need that. Just hearing you and seeing your walk through is enough.

  • @Tarodev

    @Tarodev

    Жыл бұрын

    No offence taken, this is your opinion. Can't please everyone. I suppose if you make a tutorial channel, you can create videos exactly to your liking?

  • @berkucar8420
    @berkucar84202 жыл бұрын

    There's no point in this video as you don't show how this system works on the canvas.

  • @Tarodev

    @Tarodev

    2 жыл бұрын

    I'm sorry it wasn't what you expected (a canvas 101 tutorial, I assume)

  • @zelderon7195
    @zelderon7195 Жыл бұрын

    NullReferenceException: Object reference not set to an instance of an object PuzzlePiece.OnMouseUp () (at Assets/Scripts/PuzzlePiece.cs:47) UnityEngine.SendMouseEvents:DoSendMouseEvents(Int32) (at /Users/bokken/buildslave/unity/build/Modules/InputLegacy/MouseEvents.cs:208)

  • @uroojfatima9169
    @uroojfatima9169 Жыл бұрын

    NullReferenceException: Object reference not set to an instance of an object PuzzlePiece.OnMouseUp () (at Assets/Scripts/PuzzlePiece.cs:44) UnityEngine.SendMouseEvents:DoSendMouseEvents(Int32) Facing this error

  • @Sam-pnk

    @Sam-pnk

    Жыл бұрын

    I'm facing the same problem! How did you fix it? :(

  • @zelderon7195

    @zelderon7195

    Жыл бұрын

    I have the same problem

  • @uroojfatima9169

    @uroojfatima9169

    Жыл бұрын

    ​@@Sam-pnkapplied some conditions . i'll share my puzzle piece.cs

  • @user-kp5cp8ed3e

    @user-kp5cp8ed3e

    8 ай бұрын

    @@uroojfatima9169 how did you solve it?