ไม่สามารถเล่นวิดีโอนี้
ขออภัยในความไม่สะดวก
Advanced Drag & Drop system for your Game in Unity!
ฝัง
- เผยแพร่เมื่อ 16 ส.ค. 2024
- Let's make a complete Drag & Drop system in Unity, working on both Computer and Mobile (Android, iOS) with extra features such as:
1) Avoiding overlapping items by repositioning in the closest valid position
2) Snapping inside another item if the drop is valid
3) Going to the starting position if the drop is not valid
This video will start from a scenario where you already have a basic Drag & Drop system implementation. If you need that as well, feel free to watch the part one here: • Drag and Drop for Mobi...
_______________________________
Hit like and subscribe for more content! :D
You can also follow me on the other platforms:
Twitter: / balastrong
TikTok: / balastrong
Instagram: / devbalastrong
dev.to: dev.to/balastrong
GitHub: github.com/Bal...
_______________________________
TIMESTAMPS
0:00 - Introduction
0:41 - Auto reposition to avoid overlapping
3:20 - Valid and Invalid drop handling
4:48 - Epic fail - Use contants!
5:04 - Final demo
5:25 - Like and subscribe
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!
Thank you. Your voice and attitude is so chill - I really enjoyed this tutorial.
What's up, GameDevs?
I'm sorry if it took a little bit longer than expected to publish the second part of the tutorial, but finally, here it is!
Feel free to suggest any topic for future videos, I'd like to give priority to community request :)
See you in the GameDev journey!
It would be super helpful if you could publish the source code in the comments or description for newbies like me that get stuck
Great Tutorial, thank you so much for sharing it with us!
Hey Carles, welcome to the channel! That's great to hear you enjoyed the tutorial! Did you also watch the part one or you started from this one? :)
@@DevLeonardo Yes I have seen the 1st one also. I have been looking for this system all the week and then I found you!!
You're welcome :P
Late comment, but if it helps anyone - when setting the Dragging layer number in Layer at the very start of the video, make sure the number you set matches the layer number in the inspector! I just picked a random slot and didn't realise the number coincided with what is typed into the Layer script! DevLeo used slot 8 so in Layer script it must say slot 8 not a different slot etc.
Hey! not sure but on my side, i have a Bug where in, while collision, it doesn't update the Item to Move away from each other, it only updates when i click on another game object.
same here!
Same here
Hey guys, thanks for letting me know! Maybe I have skipped something in the video... I'll check and get back to you soon!
Same ☹️
@@DevLeonardo Same here... it does check if its colliding, but its not doing anything after that
Is there a way I can skip the auto reposition part? thanks.
hey i have a question on how do I want to make, for an example i want to make gem can only insert at green but not at red and the coin can insert to red but not at green ?
Welcome to my channel and thanks for the question!
In your OnTriggerEnter2D you have a reference to the other object you collided with. You can assign a script to the boxes with a public list of strings of items that can be inserted.
Inside the function, you will just check if the items that you're dragging is in the allowed list :)
@@DevLeonardo So what you mean is that i need to assign list of item that can be inserted in the if(other.comparetag) right ?
Bernardo, now I have a question about your video:
1 what to add to the script so that the sprites are very smoothly attracted(glued). Was smooth pull (sprite gold)? And:
2 What and where to change in the script so that the gold (sprite) can no longer be pulled out of the box. So that two sprites (gold and a box) become like a background?
hello! thanks for the great video ! just wanna ask, for the image that we want to drag, is it need to choose image in UI at the hierarchy at the left side? hope you'll answer this. thanks :))
Great video!
I have an issue with my sprite snapping to a z location off screen when i click down, in the inspecter its last position is z -641009.9 every time (no console errors and scripts, tags, rigidbodys, and colliders attached). Any ideas and Id be exceedingly grateful haha :)
how do you make the sprites position stay inside the screen space of any resolution or size screen?
you need to configure your canvas. In render mode you set "Screen space - overlay", and set the canvas scaler, in UI scale mode to "Scale with screen size".
How... Drag & Drop way of system UI
Doesn't work. The colliding of two object doesn't work at the end of the drag, it works at the start of one.
He made an error in the code. Inside the Update method of DragController, he used Input.GetMouseButtonDown instead of Input.GetMouseButtonUp
Hello
How to make a sprite come to the front on mouse click? Please 🙏
You can change it's sorting layer to one on top and then return it to the original
@@bernardoamim9601 I understand in words
But I don’t understand how it should look
Can you help?
I would like to do as in this video:
th-cam.com/video/h9R8M65V9H4/w-d-xo.html
@@lora6938 Sure. The Game Object that contains the Sprite has a Sprite Renderer component in the Inspector. If you look at the bottom of the Sprite Renderer Component section, you'll see an "Additional Settings" dropdown with two fields: 'Sorting Layer and Order in Layer'. To make the dragged object go on top of everything else, you need to set it to a sorting layer with a higher order while dragging and then return it to the original sorting Layer
@@bernardoamim9601 Can you help me do it as in the video. th-cam.com/video/h9R8M65V9H4/w-d-xo.html
What to write in the script and how to set it up later?
@@lora6938 Nice! You're almost there! Only a few tips:
- On your sorting layers, click "Add Layer". You'll have some sorting layers there. Something like "Layer 1, Layer 2".
- Rename one of them to "Sprites" so that you know which you should work with.
- On you Sprites, go to the Sprite Renderer at the inspector and change the Sorting Layer to be the one we just created
- On the "Sorting Order" field, you can leave it at zero for now.
- On your script, you are only working with the mouse down event, but what we need to do is set the sorting order to a higher number while dragging and return it back to zero at drag end.
- So try this:
OnMouseDown ()
{
gameObject.GetComponent () .sortingOrder = 10;
}
OnMouseUp ()
{
gameObject.GetComponent () .sortingOrder = 0;
}
If all of your sprites are default setted with "Sprites" sorting layer, and sorting order = 0, it should be working now