Drag and Drop in Unity - 2021 Tutorial

Поделиться
HTML-код
  • Опубликовано: 24 янв 2021
  • Learn how to quickly drag and drop sprites and 2D objects in Unity. I'll show you how it works and some neat tricks you can do with it.
    ❤️ Become a Tarobro on Patreon: / tarodev
    =========
    🔔 SUBSCRIBE: bit.ly/3eqG1Z6
    🗨️ DISCORD: / discord
    ✅ MORE TUTORIALS: / tarodev
    About Tarodev:
    Develop video games like a pro!
    Whether you're a beginner or a seasoned game developer, you'll find lots of useful tips and tricks to boost your development career. Visit the channel: / @tarodev
    Dragger.cs: bit.ly/3a4gqRR

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

  • @abhimanyuta9367
    @abhimanyuta9367 2 года назад +14

    This was very useful! Do continue making videos in this style. Clear and concise, and extra points after clearing up the main objective.

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

      Glad you enjoyed it 👍

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

    So happy to find this video, the most simple tutorial about drag & drop

  • @user-vd2kz6tp2i
    @user-vd2kz6tp2i 3 года назад +1

    Thank you! for telling me what I didn't understand in game development

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

    omg, why didn't I find this sooner - I thought the offset in dragging just wasn't possible to fix and I watched so many tutorials. perfect tutorial even for a non-programmer like me, thank you!!

  • @larryp2206
    @larryp2206 2 года назад +22

    For those of you trying this out yourself, you must set the camera's 'Projection' to 'Orthographic' or this tutorial won't work. Alternatively, if you leave the camera's Projection set to 'Perspective' you must set the z component of the vector returned by Input.mousePosition to the distance between the camera and the sprite which will take a bit of calculation.

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

      Doesn't work even then, unfortunately... No errors occured to me, but the object I'm trying to move doesn't move at all.

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

      @@MrSagaTeller make sure your object has a collider

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

      @@MrSagaTeller check not to have two BoxColliders on object. This was my issue with this simple and excellent tutorial.

  • @SmellGrow
    @SmellGrow 9 месяцев назад +2

    ThankYou! i needed a tutoral this eazy!

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

    Fantastic tutorial

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

    thank you! works very nicely

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

    Excellent - thank you

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

    This was a great tutorial, thank you very much.

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

      You're very welcome :)

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

    Great video. Im not sure if was intentional or not, but here, in my project, I compare not only the distance, but the also the sprite. I want the conection only with the right Slot. Congrats!

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

    Thanks! Very clear and effective instruction.
    Whenever you have time, it would be very interesting to show us how a matching game for young children (e.g. a duck image to the word duck) can be implemented using your excelent drag and drop process.

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

      Great suggestion! I'll might do this next :)

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

    Camera.main is automatically cached after Unity 2020 as it should be million years ago,
    so if you are using versions after that just don't worry about it anymore.

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

    Thanks!

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

    great video👍

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

    Hi, thanks for the video. How can I lock the position after the object dropped? I mean not be able to pick it up again?
    thank you in advance!

  • @bobgamernoobq4776
    @bobgamernoobq4776 3 года назад +1

    you are the best ever

  • @bobgamernoobq4776
    @bobgamernoobq4776 3 года назад +1

    thx you sooooooooooooooooooo much :D

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

    You helped me out so much!
    I'm in a hurry for a game jam, and I started late.

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

      Good luck on the jam brother!

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

      ​@@Tarodev Thanks!

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

    thanks :)

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

    I have two sprites overlapping. One has this script with OnMouseDown draggable and another has a BoxCollider2d where isTrigger is true. Sometimes I can drag the sprite other times I cannot. Any thoughts on how to fix this?

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

    Can I do this for multiple finger drag with multiple object into unity Canvas ?
    Please help me with that problem ! thanks

  • @adamsekerka3428
    @adamsekerka3428 3 года назад +1

    Love u man

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

      Yeah babe ;)

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

    Thanks for great guide! But how can we understand we release it? Because i want to do smth else when we release, stop dragging it

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

    Hello! Could you show Drag and Drop over the network especially through the Mirror network!? Please!🙏🙏🙏

  • @manzell
    @manzell 3 года назад +1

    Is this event preferable compared to the IDragDropHandler's built in to the EventSystem?

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

      That's for UI. I would certainly use the IPointerEvent handlers for UI which have no colliders.

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

    Error: The name 'Camera' does not exist in the current context. What could be the reason?

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

    Hello! Do you know how to do that when you release the gray sprite, it rolls smoothly and lands itself
    I know how to do it in dynamics, but I don’t know how in kinematics. (body type)

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

      Kinematic is removed from the 'general' physics environment and is only allowed to move using explicit instructions from the user (ie, controlling the body via script). If you need the object to be affected by physics you'll need to use dynamic.

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

      @@Tarodev Yes, everything is there, my sprite rides smoothly after releasing the mouse, but only in dynamics, but not in kinematics. Is it possible to do this for kinematics?

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

    Guys making tutorials. You really should learn how to zoom in your video to show only the part of the screen with the script. Because when your script is just a quarter of your screen it's really hard to see anything. Thanks for the video.

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

      I do so in more recent videos. This was one of the first tutorials I ever made 😜

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

    I don't get it, How does it know which object are you dragging? Since both of them looking for mouse input. I'm new to coding by the way.

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

      Box Colliders and 2D hierarchy ordering :)
      Colliders lets the mouse interact with the sprite (like a trigger zone)
      and the Order deturmines whjat sprite is on top
      I think by default it the colliders stop "clickthrough", so you dont click and grab both, just the top sprite :)

  • @JohnDoe-pk8lc
    @JohnDoe-pk8lc 2 года назад +1

    🅱️ery nice

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

    it doesn't seem to work for me?
    I copied the script and gave it to an object in my 2D project, no errros or anything, but it just doesn't do anything

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

    is this code only work in Unity2D? I try the code in Unity3D but it wont work. Pls let me know if you got the way.

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

      Hi KA 豪:D
      try this it will work for both 2D and 3D
      Vector3 GetMousePos()
      {
      var pos = new Vector3(Input.mousePosition.x, Input.mousePosition.y,
      _cam.WorldToScreenPoint(transform.position).z);
      //var mousePos = _cam.ScreenToWorldPoint(Input.mousePosition);
      var mousePos = _cam.ScreenToWorldPoint(pos);
      mousePos.z = 0;
      return mousePos;
      }

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

      You must set the camera's 'Projection' to 'Orthographic' or this won't work

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

    how to make the dragged object be on top and not go behind the other objects, like here at 5:30?

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

      Look into sorting order on the sprite renderer

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

      @@Tarodev Thanks for answering, I'm new to unity so don't know if what I did is the correct thing:
      1. I've tried to change the order in the same layer with sprite.sortingOrder, didn't work.
      2. tried to assign the object to a new higher layer, also didn't work.
      I guess it has something to do with the sorting in the UI, with how the hierarchy works. so what I did is that I changed the parent of the dragged object to a canvas that's above the old parent in the UI hierarchy. It did work but created a different issue.

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

    cool, now do that again with the new input system...please. :)

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

      Very good idea!

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

      @@Tarodev did u do it

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

    dont know why it doesnt work. i tried both box collider and box collider2d

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

      You must set the camera's 'Projection' to 'Orthographic' or this won't work

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

    Cool tutorial but i need help with an error: NullReferenceException: Object reference not set to an instance of an object
    firstchalange.GetMousePos () (at Assets/firstchalange.cs:15)
    firstchalange.OnMouseDrag () (at Assets/firstchalange.cs:10)
    UnityEngine.SendMouseEvents:DoSendMouseEvents(Int32)
    Can somewhone help me please

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

      Although it's been a long time, but if you need it, make sure to set the camera settings like a movie, and don't forget cam=camera.main