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
This was very useful! Do continue making videos in this style. Clear and concise, and extra points after clearing up the main objective.
Glad you enjoyed it 👍
So happy to find this video, the most simple tutorial about drag & drop
Thank you! for telling me what I didn't understand in game development
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!!
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.
Doesn't work even then, unfortunately... No errors occured to me, but the object I'm trying to move doesn't move at all.
@@MrSagaTeller make sure your object has a collider
@@MrSagaTeller check not to have two BoxColliders on object. This was my issue with this simple and excellent tutorial.
ThankYou! i needed a tutoral this eazy!
Fantastic tutorial
thank you! works very nicely
Excellent - thank you
This was a great tutorial, thank you very much.
You're very welcome :)
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!
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.
Great suggestion! I'll might do this next :)
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.
Thanks!
great video👍
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!
you are the best ever
thx you sooooooooooooooooooo much :D
You helped me out so much!
I'm in a hurry for a game jam, and I started late.
Good luck on the jam brother!
@@Tarodev Thanks!
thanks :)
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?
Can I do this for multiple finger drag with multiple object into unity Canvas ?
Please help me with that problem ! thanks
Love u man
Yeah babe ;)
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
Hello! Could you show Drag and Drop over the network especially through the Mirror network!? Please!🙏🙏🙏
Is this event preferable compared to the IDragDropHandler's built in to the EventSystem?
That's for UI. I would certainly use the IPointerEvent handlers for UI which have no colliders.
Error: The name 'Camera' does not exist in the current context. What could be the reason?
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)
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.
@@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?
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.
I do so in more recent videos. This was one of the first tutorials I ever made 😜
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.
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 :)
🅱️ery nice
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
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.
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;
}
You must set the camera's 'Projection' to 'Orthographic' or this won't work
how to make the dragged object be on top and not go behind the other objects, like here at 5:30?
Look into sorting order on the sprite renderer
@@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.
cool, now do that again with the new input system...please. :)
Very good idea!
@@Tarodev did u do it
dont know why it doesnt work. i tried both box collider and box collider2d
You must set the camera's 'Projection' to 'Orthographic' or this won't work
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
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