How to make a drag and drop game in Unity

Поделиться
HTML-код
  • Опубликовано: 3 июн 2024
  • 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

Комментарии • 88

  • @rhalferty
    @rhalferty Год назад +4

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

  • @daniellaplanagimeno3927
    @daniellaplanagimeno3927 6 месяцев назад +4

    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
    @jadonmullinex9389 2 года назад +3

    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!

  • @fuzeworksit6465
    @fuzeworksit6465 Год назад +5

    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! 💪💪

  • @user-bf7xm5bo1d
    @user-bf7xm5bo1d 7 месяцев назад +1

    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.

  • @vietanhnguyen1415
    @vietanhnguyen1415 2 года назад

    Thanks man, your video is my lifesaver

  • @alanadonascimentofreitas4904
    @alanadonascimentofreitas4904 2 года назад

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

  • @bluepenguin_01
    @bluepenguin_01 Год назад +1

    great tutorial, thanks man

  • @amelialewey9221
    @amelialewey9221 Год назад +1

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

  • @hdggfhjb6768
    @hdggfhjb6768 2 года назад +1

    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?

  • @keziavb
    @keziavb 3 года назад +3

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

    • @Tarodev
      @Tarodev  3 года назад

      Glad I could teach you something :)

  • @Ottiya
    @Ottiya 2 года назад +4

    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
    @samilaferreira5658 2 года назад

    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.

  • @mactram7293
    @mactram7293 3 месяца назад

    tk very much, it help me so much

  • @ibrahemslman8159
    @ibrahemslman8159 Год назад

    you are my hero

  • @johnangelomanuel1967
    @johnangelomanuel1967 2 года назад

    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.

  • @noobcraft5712
    @noobcraft5712 3 года назад +3

    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

  • @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!!!

  • @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?

  • @shuganyaravi6765
    @shuganyaravi6765 Год назад

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

  • @JudasBricott
    @JudasBricott 2 года назад

    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.

  • @MstrDennis777
    @MstrDennis777 2 года назад

    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?

  • @Aurillia
    @Aurillia 2 года назад +3

    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 года назад +2

      You're abusing the power!!

  • @katilehtonen614
    @katilehtonen614 10 месяцев назад

    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?

  • @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..

  • @michael.corlenome
    @michael.corlenome 2 года назад

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

  • @nehal_chandra2777
    @nehal_chandra2777 2 года назад +5

    my brain is too dirty for this

    • @micaiahstevens8840
      @micaiahstevens8840 2 года назад +1

      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.

  • @dragonarc_gaming
    @dragonarc_gaming 11 месяцев назад

    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.

  • @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

  • @jayskylark8692
    @jayskylark8692 Месяц назад

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

  • @user-rj4ef8dv9o
    @user-rj4ef8dv9o 8 месяцев назад +1

    where i can get those auido source?

  • @datd4063
    @datd4063 2 года назад +2

    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  2 года назад

      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 2 года назад

      @@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 2 года назад

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

    • @Tarodev
      @Tarodev  2 года назад

      @@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 2 года назад

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

  • @wasab1z
    @wasab1z 2 года назад

    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.

  • @georgebowen2555
    @georgebowen2555 Год назад

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

  • @andubunea9627
    @andubunea9627 3 года назад +6

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

    • @Tarodev
      @Tarodev  3 года назад +4

      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

  • @Agentti_Kowalski
    @Agentti_Kowalski 11 месяцев назад

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

  • @ShyDeaw
    @ShyDeaw 3 года назад

    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.

  • @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.

  • @doctortruth1179
    @doctortruth1179 9 месяцев назад

    Can I put that on canvas?

  • @navodyaliyanage6297
    @navodyaliyanage6297 2 года назад +2

    can u plz share the github link as well?

  • @lirikshack7672
    @lirikshack7672 6 месяцев назад

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

  • @mousamohamed5789
    @mousamohamed5789 2 года назад

    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.

  • @user-rj4ef8dv9o
    @user-rj4ef8dv9o 4 месяца назад

    can u make a Visual Scripting verson of this ?

  • @choiscoc_tv1000
    @choiscoc_tv1000 Год назад

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

  • @bharatgupta1937
    @bharatgupta1937 Год назад

    Guys can someone help me with the sprites folder?

  • @yannickgervais240
    @yannickgervais240 10 месяцев назад

    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 10 месяцев назад

      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.

  • @Zack01OFFICIAL
    @Zack01OFFICIAL 2 года назад

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

  • @muhammadzahirbinsubakhir7995
    @muhammadzahirbinsubakhir7995 2 года назад +1

    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.

  • @avia4281
    @avia4281 6 месяцев назад

    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.

  • @nikomationsproblems
    @nikomationsproblems 11 месяцев назад +1

    Can You Add Numbers Game To Google Play?

  • @humayrabakar4207
    @humayrabakar4207 2 года назад

    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"?

  • @berkucar8420
    @berkucar8420 2 года назад +1

    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)

  • @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  Год назад +1

      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?

  • @uroojfatima9169
    @uroojfatima9169 Год назад +2

    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 10 месяцев назад

      I have the same problem

    • @uroojfatima9169
      @uroojfatima9169 10 месяцев назад

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

    • @user-kp5cp8ed3e
      @user-kp5cp8ed3e 6 месяцев назад

      @@uroojfatima9169 how did you solve it?

  • @zelderon7195
    @zelderon7195 10 месяцев назад

    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)