Easy Drag and Drop with Input System 2D | 3D - Unity Tutorial
HTML-код
- Опубликовано: 22 май 2024
- This video shows an easy yet extendable way to implement drag and drop using the new input system in unity.
📥 Get the Source Code 📥
/ 58031139
🤝 Support Me 🤝
Patreon: / samyg
Donate: ko-fi.com/samyam
►🔗 Relevant Video Links 🔗
ᐅCamera.ScreenPointToRay
docs.unity3d.com/ScriptRefere...
►Get Object from Mouse Click and Call Functions through Interface 2D & 3D - Unity Tutorial
• Get Object from Mouse ...
►How to use Unity's Input System
• How to use Unity's Inp...
In this video we use Unity's new input system to implement dragging and dropping of both a 3d object and 2d object (2d collider). We use raycasts to get our object ScreenPointToRay, which takes into account the camera perspective easily, and then start a coroutine where we check each frame to see in what direction to move in. I'll show you how to implement this with physics to allow the object to stop when colliding with others, and also fall when released. I also show you how to use an interface to perform other actions once we've begun and ended dragging a gameobject.
Disclosure: This post may contain affiliate links, which means we may receive a commission if you click a link and purchase something that we have recommended. While clicking these links won't cost you any money, they will help me fund my development projects while recommending great assets!
😎 Other Cool Unity Assets 😎
ᐅPeek - Editor Toolkit: assetstore.unity.com/packages...
ᐅEditor Console Pro: assetstore.unity.com/packages...
ᐅRainbow Folders 2: assetstore.unity.com/packages...
►⏱️ Timestamps ⏱️
0:00 Intro
0:25 Install Input System
0:42 Scene Setup
0:56 Drag and Drop Script
10:43 Move Object without Physics
11:51 Set Up Script
14:03 Set up Tag or Layer for Draggable Objects
15:37 Interfaces
19:51 2D Colliders
20:55 Outro
💖💖THANK YOU TO ALL MY PATRONS 💖💖
❯❯❯ My Links ❮❮❮
💗 Patreon 💗
/ samyg
💬 Discord Server 💬
/ discord
🐦 Twitter 🐦
/ samyam_utube
📚 Facebook 📚
/ samyam.youtube
🎵 Music 🎵
Ambient Gold
streambeats.com
👍 Like and Subscribe! 👍
🖥️ Computer Setup 🖥️
*As an Amazon Associate I earn from qualifying purchases.
www.amazon.com/shop/samyam
#madewithunity #gamedev #unity - Наука
honestly one of the more well thought out and explained unity tutorials ive watched, i learned so much from this
Amazing tutorial. Clear and to the point without sacrificing any needed explanations. You're a great teacher. I'm working on my first project in Unity and you answered so many questions for me. Thanks for taking the time to make videos and sharing them with all of us.
Thanks so much for the nice comment!
Thank you so much! Implementing this functionality with the new input system was driving me nuts for days! This worked perfectly!
Thanks a LOT for this video, I was looking exactly for how to work out this functionality on the new Input system. This video was so well layered out and explained, that not only it gave me a lot of insight about the subject, but also really specific techniques while at it as well! Really appreciated!
Awesome! Glad you found it useful :)
Oh I'm actually learning this and the UI drag and drop. I look forward to it!
Thank's a lot for your tutorials @samyam. And thank's Google for recommending me this channel :)
Yay! Thanks so much 😄
Thank you SO MUCH! Keep up the excellent work!
Great video, it's very informative. Thank you !
queen of uncomplicated tutorials 💘
Great Unity tutorial - well articulated and helpful!
Thank you!!
Super supremely awesome. Definitely subscribed after this video.
Thanks!!
Helped me a lot! just one note, you need to add a collider programmatically if you have a custom mesh added at runtime.
Thank you for the video!
Hi Samyam,, Great tutorial - it's clear, to the point and well-conceived. Keep up the good work! Quick question - How can I use this script on an imported OBJ object? It works fine with native Unity objects but does not seem to work ( at least for me ) with imported ones. Any help would be appreciated.
Damn this was good as a cold beer en la playa babe! tysm!
Great tutorial I learned a lot today.
Awesome! Glad you liked it 😁
Looks cool:3
Great Video! I have a question: Will this code work if I build and run it on the android platform? Thank you
Easy Peasy Lemon Squeezy 😎🤝
Saving this 4 later
Good tutorial. Some of these functions are really obtuse and impossible to figure out without good documentation or tutorial. I mean, for example why do they store whether the mouse button is clicked or not as a float of all things? As if there are any analog mouse buttons...
How do stop from being able to fling items across the map? For instance I want it so that as soon as I stop dragging, the item stops and drops to the floor.
Why do you prefer comparing ReadValue() to 0 over using the IsPressed() method?
Which binding(s) do we use for touch?
Very helpful tutorial! 😊 I would like to ask you how I could scale or rotate an object using the Input System
Thanks! You can access other mouse properties, here’s a list:
docs.unity3d.com/Packages/com.unity.inputsystem@1.0/api/UnityEngine.InputSystem.Mouse.html
And change the transform of the object respectively. Or you can use an Input Action Asset to store your actions and then read those values ruclips.net/video/m5WsmlEOFiA/видео.html
@@samyam Thanks! I will try to do it as soon as possible 😊
Awesome stuff, but I have a question: if we want to not move object along depth axis, eg z how to do that? Get angle between last two rays and using trigonometry get new position of object?
If you aren’t using physics you can set the z position to equal the previous z position of the object. If physics you can lock the Z movement constraint in the Rigidbody component settings
docs.unity3d.com/ScriptReference/Rigidbody-constraints.html
@@samyam I think I didn't explain it fully: the main problem is if you lock depth axis movement of dragged item, it is not fully following cursor, the farther you go towards edges the more it stays behind. My assumption was it is because ray should become longer if you move from center of screen towards edges That's why I thought I have to use some geometry algebra . Am I correct?
This was very well explained and although quite up-tempo, very informative. I understood your code fully and was able to replicate it. I'm not sure I'd be able to do it on my own though. How do you get this good at this?
Lots of practice and experience! Glad you liked the video :)
Hi, thank you for this video, very useful.
I just have a little problem, I can drag the floor which has no tag, no layer, no script.
Can't find why, my "if"s look like yours.
M. D.
the "{" open bracket sign is in the side if close parenthesis
Amazing tutorial, thank you so much. However, I keep having this error with your script....:Assets\DragandDrop.cs(7,6): error CS0246: The type or namespace name 'Serializefield' could not be found (are you missing a using directive or an assembly reference?)
And when I use public variables instead, it does seem to work, but at once I fall through my terrain when I click on any layer other than draggable...
Would anyone have a suggestion?
SerializeField - caps sensitive
Thanks!
@@samyam Thank you so much, you are my hero of the day!
dammm this tutorials are the best, hope you dont leave unity :((
Does it work with touch too?
Thanks again. Your vid start out with great topics, but often lean into needless complexity. Good vid, but tmi for grasping input systems usefully. This example would change the flow of most code but beggars aught not be choosers,. >KISS > 1.Keep.2.It.3.Simple.4.Stupid!
I have this problem where every time I click on an object it moves further away from the camera
Quick question, but how could I get this working on touchscreens?
You can replace the mouse coordinates with your finger coordinates! I have a video here:
ruclips.net/video/ERAN5KBy2Gs/видео.html
@@samyam If I do that, will I be able to drag multiple objects then at the same time using multiple fingers? I want to know before I start implementing that.
Does this work in unity 2d?
Just so you know, this doesn't seem to work with 2D skined characters/object. I don't know why.
Wow do you ever pack a lot of material into 20 minutes.
Is the picture at the end actually you? Also, kudos for the hidden leaf shirt I dig it. Thanks for the tutorial I learned a lot!
Thanks! Yes it’s me, probably was at my Naruto phase 🤣
@@samyam Nothing wrong with a Naruto phase, it's a great little story. You rock!
Will this work with touch Screen?
No, this is only for cursor.
21:47 thats me beaches
Hehe thanks for the support!!
HOW TO RUN THIS IN ANDROID
You'll need to add touch input to your actions, I have a video on Touch Input that might help you: ruclips.net/video/ERAN5KBy2Gs/видео.html
typed way to fast but other than that its good
My MousePressed() function just doesn't trigger. mouseClick.performed doesn't trigger? I put Debug.Log("!") in the function and it keeps silence (( Can't figure out where is the problem :(
Oh, God, I added Binding to the MouseClick action while PlayMode was running, so it wasn't saved