Unity - Drag and Drop with Snap

Поделиться
HTML-код
  • Опубликовано: 19 июн 2024
  • Learn how to implement DRAG and DROP system in the Unity with snap to point feature.
  • НаукаНаука

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

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

    Out of all the tutorials I've watched, this worked for me. So, I thank you.

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

    Just what I needed for my project, thank you for making this video!

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

    Thanks for this DRAG and DROP system in Unity to a snap point :) Just what I was looking for :)

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

    Thank you very much, this tutorial is to underated!!!

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

    Thank you very much, that was simple and very helpful

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

    Awesome tutorial! simple and well explained, i was wondering, if i wanted to do this with an isometric grid, how would i go about doing that?

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

    Great tutorial! Led me away from a crazy idea. :)

  • @sonicboomVR
    @sonicboomVR 2 года назад +8

    It's too bad this code isn't on Github or even in the comments. I've typed it exactly and get no compile errors, however it doesn't work. The Draggable script won't work when attached to a GameObject even with a BoxCollider 2D component.

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

    Thanks works perfectly

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

    This is an awesome tutorial. By far the best explanation of this I've seen. I've been trying to modify it though. I'm trying to create a chess game, and I don't feel like creating 64 objects for snap points. Instead, I think I want to use my auto-generated array of Vector2's rather than a list of Transforms, so I don't need 64 more objects. But everything I've tried so far hasn't worked.

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

      also I'm also having weird issues with the draggable object becoming more and more detached from my mouse the further away I get from 0, 0.

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

      @@lavaa7392 Yes, this is due to Camera's z position

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

    for anyone wondering, there is a different way to make that delegate by using -> public static event Action onDragEnded; (and yes, you need to set in also inside the onEnable/onDisable)

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

    well i did get it working on its own but i need it to work on a drop down box and to be able to take it off the caption image then choose another sprite put it to the top of the list and be able to drag and drop that one but having trouble just getting it to drag it off the caption image position any ideas???

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

    Does this work on a First Person View game instead of just 2D?

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

    Just a small tip. At around min 3:35 u select every single GameObject to add scripts. You could just select all by selecting the first and shiftclicking on the last and then add components so it gets added to every component

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

    This was very helpful, thank you. What i want to know is how i would get the food back to the original position and also how would you go about making a system to know when each food is in the correct position? Oh and i subbed as well :)

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

      I'm glad you liked it. You can save the original position in the Start method, and then use it inside onDragEnded method for example. To check if each dish is in the correct position, you could compare the destination points(grey circles) positions with the dishes positions.

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

    if anyone has an error with each "Draggable"
    and an erroe with "this"
    then you have to change each "Draggable" in your code to the name of the mouse dragging script

    • @vc.2023
      @vc.2023 10 месяцев назад

      I can't drag the object, what do you mean about "this"?, can you show me the script😢, many thanks

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

    Thank for tuttorial
    How can i add a win popup and new levels? Thank

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

    Thanks

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

    can we do this on a 3d project?
    @Curious Bits

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

      for 3d use this code
      private void OnMouseDown()
      {
      isDragged = true;
      mouseDragStartPosition = Camera.main.ScreenToWorldPoint(Input.mousePosition);
      playerDragStartPosition = transform.position;
      }
      private void OnMouseDrag()
      {
      if (isDragged)
      {
      Vector3 position = new Vector3(Input.mousePosition.x, Input.mousePosition.y, Camera.main.WorldToScreenPoint(transform.position).z);
      Vector3 worldPosition = Camera.main.ScreenToWorldPoint(position);
      transform.position = new Vector3(worldPosition.x, 0.5f, worldPosition.z);
      }
      }
      private void OnMouseUp()
      {
      isDragged = false;
      }

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

    Do you need to put an image to the snap points? Mine won't snap even though I followed the code

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

      I'm also having trouble just using GameObjects with RectTransforms, but there's no sprite dependency in the code at all.

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

      Its weird, no-one seems to be talking about this, it moves fine, but doesn't snap. Plus im kind of unsure about what to use this for, my original intention was to have items snap to an object then when the item is in range and snapped have the items "recharge", i guess ill just do it with a collider looking for a tag, lol.
      But I now realise that this method doesn't set a variable on the slot to "Occupied", so I'm unsure if this would even fit my needs even if it did work, lol, as I'd want the slot to become the object's parent too...

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

    Does this work in 3D?

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

    thank u for te tutor. but can u help me to show "you win" text and some button to next level? thank u so much

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

    so not sure what I have done wrong but the objects will not drag it goes threw the code but will not drag I have the script and the box collider on the sprite but it will not drag??? did u put any thing on the background is the sprite attached to a empty game object or to a game Object cube I have looked it over 20 times nothing

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

      found it it needed to be in in orthographic mode

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

      YESS thank you! :D

  • @vc.2023
    @vc.2023 10 месяцев назад

    I try the first dragging part, it doesn't drag at all? What happens?

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

    can you give the script in the description

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

    If the slot is busy. I search for many and many videos and not yet. No one show how validate if slot is busy.

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

      There are a lot of ways to implement this. For example by using a boolean that checks if the snapPosition is being occupied, if so you can skip a cycle inside the foreach loop if that occurs

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

    This is driving me mad I've followed this as perfectly as I can see and the snapping doesn't work. I've used this tutorial a few times since it was made and it's never worked :S

    • @isol8id
      @isol8id 6 месяцев назад +1

      Ok I solved my version of the problem if this helps anyone. I organised my draggable objects and snap points into different parents positioned in different places and this threw off the snapping with an offset. The parents objects MUST all be at 0,0,0.