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.
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.
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)
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???
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
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 :)
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.
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
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...
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
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
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
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.
Out of all the tutorials I've watched, this worked for me. So, I thank you.
Just what I needed for my project, thank you for making this video!
Thanks for this DRAG and DROP system in Unity to a snap point :) Just what I was looking for :)
Thank you very much, this tutorial is to underated!!!
Thank you very much, that was simple and very helpful
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?
Great tutorial! Led me away from a crazy idea. :)
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.
Thanks works perfectly
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.
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.
@@lavaa7392 Yes, this is due to Camera's z position
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)
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???
Does this work on a First Person View game instead of just 2D?
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
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 :)
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.
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
I can't drag the object, what do you mean about "this"?, can you show me the script😢, many thanks
Thank for tuttorial
How can i add a win popup and new levels? Thank
Thanks
can we do this on a 3d project?
@Curious Bits
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;
}
Do you need to put an image to the snap points? Mine won't snap even though I followed the code
I'm also having trouble just using GameObjects with RectTransforms, but there's no sprite dependency in the code at all.
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...
Does this work in 3D?
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
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
found it it needed to be in in orthographic mode
YESS thank you! :D
I try the first dragging part, it doesn't drag at all? What happens?
can you give the script in the description
If the slot is busy. I search for many and many videos and not yet. No one show how validate if slot is busy.
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
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
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.