Monster shadow

VIDEO

Drag and Drop in Unity - 2021 Tutorial

Description:
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.
ADD A COMMENT

Sven Mehlhorn
thanks :)
haim ifrah
cool, now do that again with the new input system...please. :)
Tarodev
Very good idea!
John Doe210
🅱️ery nice
JazzToTheTwo
This was a great tutorial, thank you very much.
Tarodev
You're very welcome :)
WS Project
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
Julian
3:28
abhimanyu TA
This was very useful! Do continue making videos in this style. Clear and concise, and extra points after clearing up the main objective.
Tarodev
Glad you enjoyed it 👍
Unknown_content
hipity hopity this code is now my propity hehhehehe. here is the script for the people that are having errors: using UnityEngine; public class Dragger : MonoBehaviour { private Vector3 _dragOffset; private Camera _cam; [SerializeField] private float _speed = 10; void Awake() { _cam = Camera.main; } void OnMouseDown() { _dragOffset = transform.position - GetMousePos(); } void OnMouseDrag() { transform.position = Vector3.MoveTowards(transform.position, GetMousePos() + _dragOffset, _speed * Time.deltaTime) ; } Vector3 GetMousePos() { var mousePos = _cam.ScreenToWorldPoint(Input.mousePosition); mousePos.z = 0; return mousePos; } }
Adam Sekerka
Love u man
Tarodev
Yeah babe ;)
BOBGamerNoob Q
you are the best ever
BOBGamerNoob Q
thx you sooooooooooooooooooo much :D
Lora
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)
Lora
@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?
Tarodev
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.
Manzell Blakeley
Is this event preferable compared to the IDragDropHandler's built in to the EventSystem?
Tarodev
That's for UI. I would certainly use the IPointerEvent handlers for UI which have no colliders.
giokav1
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.
Tarodev
Great suggestion! I'll might do this next :)
ハロ
Thank you! for telling me what I didn't understand in game development
İlyas Köse
Thanks for sharing
Tarodev
No problem at all! Hope it helps you on your game dev journey :)
SEE ALL COMMENTS


Transcript:

all right let's implement drag and drop in unity this will be for 2d sprites
so all i've got in the scene are my sprites and
box colliders so make sure they've got box colliders on them or any kind of 2d
collider or they won't detect your mouse clicks
okay start by creating a c-sharp script called dragger or drag and drop or
whatever you want to call it open her up
okay so what we're going to do is we're going to hook into the
on mouse drag event and we're basically just going to set
our transform position equal to and we need our mouse position
in world coordinates so let's create a little function here
it's going to return a vector3 get mouse plus
and we need our camera main and what we're going to do is we're
going to get use this help function called screen to world point
what this is going to do is it's going to take a
mouse position in screen space so the y and the x and it's going to convert it
into our world space coordinates okay
so this function inputs the takes in the input dot
mouse position now because we're using just the 2d plane and this returns a
three what we're going to do is we're going to
just zero out uh our z axis okay on our z axis
um and just return basically uh a flat vector three
so all we're doing is we're setting every single frame that we're dragging
setting our position to our mouse world position
now that's should be all you need give it a try and there you go
easy drag and drop now there's a few things wrong with this
first you'll notice that when you click it it
snaps to its pivot location so so what we need to do is we need to
calculate our mouse position on click and determine the offset between the
pivot and the mouse position and then on drag
we need to add that difference so that when we grab it
it should just stay stay where we grab it
on the drag all right so let's do that so
what we need to do is get the drag offset when we
when we first click down so on mouse down and we'll
make a vector 3 and we'll call it a drag offset
and we'll set the drag offset to get mouse position
sorry transform position minus get mouse position so
it's taking the pivot or the the transform of the object
the zero and it's detect and it's where we're calculating the mouse
and we're removing the mouse position from the pivot location
okay and that there is our drag offset so now that we've got our drag offset
instead of just immediately telling it to
set it to the mouse position we'll set it to the mouse position
plus plus a newly gathered drag offset
show you that there we go much nicer feels much better doesn't doesn't have
the the snapping um a few more things that we can do to
improve this script first camera.main is a horrible thing to do
every single frame basically what this is doing is it's
searching all of our game objects and it's looking for the for the object
with tag main camera which is incredibly inefficient
so what we'll do is we'll store a reference to our camera
and then on awake we'll just we'll just grab it the one time
so camera there we go
um so that's basic drag and drop um i will show you just uh some more fun
things that you can that you can add to it instead of just
directly uh setting it to the location every
single frame what we can do is we can add a little bit of smoothing to it um
unity makes this easy uh they give you a little helper
function move towards which is effectively just learning
towards the target location every single frame
and what this needs is max distance delta so how much
we would like this to move towards the target location every frame okay so
let's create a serialized field here so we can
set it in the editor make it a float called speed give it
maybe a default of 10. we basically and then now we want to say
speed times time dot delta time we do this to keep it um
on the physics update basically uh so that
it will react the same on the computer as it would on a phone
um regardless of the frame rate um i'll show you this so they're both set
to 10. there you go we'll set the coin to uh
a bit more snappy oops a bit more snappy like that
um so you can do some fun stuff with it you could add some trials to it so that
it's making like a um a trail behind it it'll look a little
bit ugly because it uh whenever we have a texture but
you get the point um so there you go there's a
quick way to implement drag and drop if you learned anything
uh give it a like make sure maybe subscribe
and stay tuned for more