Drag and Drop for Mobile & Desktop in Unity

แชร์
ฝัง
  • เผยแพร่เมื่อ 13 ก.ค. 2024
  • In today's video, we're going to set up a simple Drag & Drop system, that works on both Mobile (Android and iOS) and Desktop and allows you to have multiple draggable items on the screen.
    This tutorial will cover the first part and we'll be able to drag and drop an item wherever we like. I'm already working on the second part where we're gonna add much more control after a drop:
    1) Snapping inside another item if the drop is valid
    2) Going to the starting position if the drop is not valid
    3) Avoiding overlapping items by repositioning in the closest valid position
    Feel free to subscribe if you're interested in the second part!
    _______________________________
    You can also follow me on the other platforms:
    Twitter: / balastrong
    TikTok: / balastrong
    Instagram: / devbalastrong
    dev.to: dev.to/balastrong
    GitHub: github.com/Balastrong
    _______________________________
    TIMESTAMPS
    0:00 - Introduction
    0:48 - Draggable setup
    1:19 - Drag Controller: Variables
    2:00 - Drag Controller: Structure implementation
    4:38 - Drag Controller: Core methods
    5:45 - Final demo
  • เกม

ความคิดเห็น • 51

  • @DevLeonardo
    @DevLeonardo  ปีที่แล้ว +1

    WATCH NEXT: Coding games alone is cool, but you know what's even better? Coding with the help of the OPEN SOURCE community!
    Learn all the benefits here: th-cam.com/video/uquIcISFtwg/w-d-xo.html it's free!

  • @surrealsnipegods3161
    @surrealsnipegods3161 3 ปีที่แล้ว +1

    it works perfectly thank you very much.
    instantly subscribed

    • @DevLeonardo
      @DevLeonardo  3 ปีที่แล้ว +1

      That's great to hear, thank you!

  • @muhammedalikoc
    @muhammedalikoc 2 ปีที่แล้ว +5

    Don't forget to change GetMouseButtonDown(0) with the GetMouseButtonUp(0) at 04:15. If not it will work properly with mouse. Thanks for the video !

  • @DevNoob
    @DevNoob 3 ปีที่แล้ว +4

    Good job! Developing for mobile can be interesting sometimes and you have to get creative. I liked your explanation

    • @DevLeonardo
      @DevLeonardo  3 ปีที่แล้ว +2

      Welcome to my channel! That's great to hear you enjoyed the video!
      If you're into mobile Dame Dev you should have a look at my video on mistakes in my mobile game, maybe you have some suggestions :P

  • @johnwachira2662
    @johnwachira2662 2 ปีที่แล้ว +1

    i like how you explain concepts. Mob love

  • @phineasfacingforward3460
    @phineasfacingforward3460 2 ปีที่แล้ว +1

    Works perfectly! Thank you so much

    • @DevLeonardo
      @DevLeonardo  2 ปีที่แล้ว

      Hey Phineas! Don't look at me with those eyes! :P
      That's great to hear it works for you, have fun!

  • @jaulloa21
    @jaulloa21 3 ปีที่แล้ว

    it works sort of. Only when I swipe the object fast to the red slot. It lerps back to previous position. Otherwise it just moves to the red slot when I move with my finger.

  • @terryscissorsgameratings8588
    @terryscissorsgameratings8588 3 ปีที่แล้ว +2

    I like this guy

    • @DevLeonardo
      @DevLeonardo  3 ปีที่แล้ว +1

      You're breathtaking! :D

  • @joonseopark3480
    @joonseopark3480 2 ปีที่แล้ว

    In a project made with NGUI, I needed a function to move an object by touch, so I tried to get help, but Raycast hit does not hit at all. I need advice. thank you.

    • @joonseopark3480
      @joonseopark3480 2 ปีที่แล้ว +1

      Sorry. I asked a question and thought about it, so I solved it. It was a simple problem that was not recognized well because the size of the collider was smaller than expected. Thanks, it worked out well. thank you.:)

  • @lora6938
    @lora6938 3 ปีที่แล้ว +1

    Hello!
    How to make sprites move smoothly?
    When you release the mouse, make them roll a little more (like curling or a puck on ice)

  • @aucer7904
    @aucer7904 3 ปีที่แล้ว

    It works well in 2d project, I try to add in the 3d project first time it doesn't work.

    • @aqueleestevao
      @aqueleestevao 2 ปีที่แล้ว

      I had to adjust two things. First, the size of my collider. It's not obvious, and probably not part of this content, but the default collider size is too small to trigger anything. Second, in Drag() the Vector2 was setting z to 0.0. In this case you need to use a Vector3 and set z to something like lastDraggedTransform.position.z. You can check the z of your gameObject.transform, but frequently it is not 0.

  • @user-cv1nx8xt8s
    @user-cv1nx8xt8s 2 ปีที่แล้ว +4

    Can I drag multiple objects at the same time with this script if I use several fingers? Thanks.

    • @legomtv7906
      @legomtv7906 2 ปีที่แล้ว +2

      I dont think so because he askes for touchcount == 1 and GetTouch(0)

    • @DevLeonardo
      @DevLeonardo  ปีที่แล้ว

      That's correct, this solution only works for one object at a time.

  • @guy5767
    @guy5767 2 ปีที่แล้ว +1

    i get error when i write line 14 in your code: DragController[ ] controller = FindObjectOfType();
    the erros is because the array you make and set it into a script which is not an array, what do i miss in the video?

    • @DRRTrsa
      @DRRTrsa 2 ปีที่แล้ว +4

      Hi, Just Change FindObjectOfType(); for FindObjectsOfType(); , you are missing the "s"

    • @guy5767
      @guy5767 2 ปีที่แล้ว +1

      @@DRRTrsa thank you, it worked :)

    • @GGamess
      @GGamess 2 ปีที่แล้ว +1

      @@DRRTrsa tnx

  • @carolinebruntsechristensen5557
    @carolinebruntsechristensen5557 ปีที่แล้ว +3

    I have done everything you said in this video, and there are no compile errors, but it simply wont move a inch when I try to drag the sprite, help...

  • @guruprasanna4801
    @guruprasanna4801 2 ปีที่แล้ว +1

    There is a offset in it, how to eliminate it

  • @tahacap8573
    @tahacap8573 ปีที่แล้ว +1

    hi! thanks for the tutorial. how to do drag and drop in 3D?

  • @JMMedinaDev
    @JMMedinaDev 3 ปีที่แล้ว

    Are you avoiding the singleton pattern for the controller to evade xplaining it?

    • @DevLeonardo
      @DevLeonardo  3 ปีที่แล้ว +2

      Hey! Welcome to the channel :)
      It wasn't strictly required here, but if you're interested in the Singleton pattern I can make a video about it, with pros and cons, and best practices on how to use them. In case you're not subscribed yet, just hit that red button so you're notified when the video will be out.

    • @JMMedinaDev
      @JMMedinaDev 3 ปีที่แล้ว

      I think It would be something interesting to see

    • @joansebastianaguilarjimene1464
      @joansebastianaguilarjimene1464 3 ปีที่แล้ว

      @@DevLeonardo Hi, I know about singleton pattern, but i think that would be interesting a video explanen "pros and cons, and best practices on how to use them".
      P.S: Your video really helped me, thanks!. Like and Suscribe

  • @gaboandres
    @gaboandres 3 ปีที่แล้ว +1

    Why do you use a script to mark the object as a draggable instead of a tag?

    • @DevLeonardo
      @DevLeonardo  3 ปีที่แล้ว

      Hello and welcome to the channel! That's indeed a valid question :)
      You can also use a Tag and things works perfectly. In this case I did that in preparation of the additional features I described in part 2 of the tutorial. You can give it a look on my channel ;)

  • @MeowKotyaKotik
    @MeowKotyaKotik 2 ปีที่แล้ว +1

    error CS0246: The type or namespace name 'Draggable' could not be found (are you missing a using directive or an assembly reference?)

  • @Diertstarr
    @Diertstarr 3 ปีที่แล้ว +1

    Dang, I'm getting an error:
    "Assets/Scripts/drag_controller.cs(58,25): error CS0119: 'draggable' is a type, which is not valid in the given context"
    Any thoughts?

    • @Diertstarr
      @Diertstarr 3 ปีที่แล้ว

      Ok, fixed it; forgot some script!
      Is there supposed to be some code in Draggable.cs?

    • @Diertstarr
      @Diertstarr 3 ปีที่แล้ว

      Ok, compiled fine, but no drag & drop for me. =(

    • @Diertstarr
      @Diertstarr 3 ปีที่แล้ว

      Have colliders on Prefab, added Draggable script; no go.

  • @mextrix1450
    @mextrix1450 2 ปีที่แล้ว +1

    Hey now someone what is Assets/UI drag.cs(14,10): error CS0246: The type or namespace name `Draggable' could not be found. Are you missing an assembly reference? bycouse i dont know

    • @JMA_unofficial
      @JMA_unofficial 2 ปีที่แล้ว +1

      you probably named the first script draggable without capital letter

  • @BrentTapes
    @BrentTapes 2 ปีที่แล้ว +1

    pls Notice me, i need to fix this
    Assets\Scripts\Game\DragController.cs(46,33): error CS0117: 'Camera' does not contain a definition for 'main'

    • @tahacap8573
      @tahacap8573 ปีที่แล้ว

      you probably have another camera or do not set your camera as the main.

  • @AyshanRza
    @AyshanRza 4 หลายเดือนก่อน

    it doesn't work(

  • @demi9672
    @demi9672 2 ปีที่แล้ว +1

    Very good. You can also create a tag "DraggableObject", and assign it to your gameobjects instead of creating an empty script.

  • @Skaski107
    @Skaski107 ปีที่แล้ว +4

    it doesnt work

  • @sugargamerr
    @sugargamerr ปีที่แล้ว

    how to fix this error Cannot implicitly convert type 'DragController' to 'DragController[]'

  • @DevLeonardo
    @DevLeonardo  ปีที่แล้ว +1

    Let's do some networking guys! :)
    We can get in touch on Twitter: twitter.com/Balastrong
    If you like reading articles, here's my dev.to profile: dev.to/balastrong
    If you're more on social networks: www.tiktok.com/@balastrong and instagram.com/devbalastrong/
    See you there!