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
  • НаукаНаука

Комментарии • 65

  • @lovelyanne0
    @lovelyanne0 Год назад +6

    honestly one of the more well thought out and explained unity tutorials ive watched, i learned so much from this

  • @FearlessMakers
    @FearlessMakers 2 года назад +7

    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.

    • @samyam
      @samyam  2 года назад

      Thanks so much for the nice comment!

  • @ralphm.881
    @ralphm.881 Год назад

    Thank you so much! Implementing this functionality with the new input system was driving me nuts for days! This worked perfectly!

  • @codeFriendlyART
    @codeFriendlyART 2 года назад +2

    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!

    • @samyam
      @samyam  2 года назад +1

      Awesome! Glad you found it useful :)

  • @ironcladvr
    @ironcladvr 2 года назад

    Oh I'm actually learning this and the UI drag and drop. I look forward to it!

  • @davidReyGD
    @davidReyGD 2 года назад

    Thank's a lot for your tutorials @samyam. And thank's Google for recommending me this channel :)

    • @samyam
      @samyam  2 года назад

      Yay! Thanks so much 😄

  • @dr.araujo-junior
    @dr.araujo-junior 2 года назад

    Thank you SO MUCH! Keep up the excellent work!

  • @busynessy798
    @busynessy798 Год назад

    Great video, it's very informative. Thank you !

  • @isabel7213
    @isabel7213 9 месяцев назад

    queen of uncomplicated tutorials 💘

  • @ClassicGameSessions
    @ClassicGameSessions 2 года назад +1

    Great Unity tutorial - well articulated and helpful!

    • @samyam
      @samyam  2 года назад +1

      Thank you!!

  • @thealetree
    @thealetree 2 года назад

    Super supremely awesome. Definitely subscribed after this video.

    • @samyam
      @samyam  2 года назад +1

      Thanks!!

  • @myfufina1
    @myfufina1 2 года назад

    Helped me a lot! just one note, you need to add a collider programmatically if you have a custom mesh added at runtime.

  • @hubbubbobbin
    @hubbubbobbin 6 месяцев назад

    Thank you for the video!

  • @ymarcellus6763
    @ymarcellus6763 Год назад +1

    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.

  • @racrity
    @racrity 2 месяца назад

    Damn this was good as a cold beer en la playa babe! tysm!

  • @pharos640
    @pharos640 2 года назад

    Great tutorial I learned a lot today.

    • @samyam
      @samyam  2 года назад

      Awesome! Glad you liked it 😁

  • @normalguy282
    @normalguy282 2 года назад

    Looks cool:3

  • @julmardelima6055
    @julmardelima6055 Год назад

    Great Video! I have a question: Will this code work if I build and run it on the android platform? Thank you

  • @rebarius
    @rebarius 2 года назад

    Easy Peasy Lemon Squeezy 😎🤝

  • @ARabbitGuy_
    @ARabbitGuy_ 2 года назад

    Saving this 4 later

  • @plasticflower
    @plasticflower 8 месяцев назад +1

    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...

  • @DemersCreations
    @DemersCreations Год назад

    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.

  • @jbaidley
    @jbaidley 2 года назад

    Why do you prefer comparing ReadValue() to 0 over using the IsPressed() method?

  • @JetBlackJester
    @JetBlackJester Год назад

    Which binding(s) do we use for touch?

  • @santinafortuna1699
    @santinafortuna1699 2 года назад

    Very helpful tutorial! 😊 I would like to ask you how I could scale or rotate an object using the Input System

    • @samyam
      @samyam  2 года назад +1

      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

    • @santinafortuna1699
      @santinafortuna1699 2 года назад

      @@samyam Thanks! I will try to do it as soon as possible 😊

  • @MarekNijaki
    @MarekNijaki 2 года назад +1

    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?

    • @samyam
      @samyam  2 года назад

      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

    • @MarekNijaki
      @MarekNijaki 2 года назад +1

      @@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?

  • @giorgostrc6878
    @giorgostrc6878 2 года назад +1

    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?

    • @samyam
      @samyam  2 года назад +1

      Lots of practice and experience! Glad you liked the video :)

  • @miaoumixed4268
    @miaoumixed4268 2 года назад

    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.

  • @IcelandicCello
    @IcelandicCello Год назад

    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?

    • @samyam
      @samyam  Год назад

      SerializeField - caps sensitive
      Thanks!

    • @IcelandicCello
      @IcelandicCello Год назад

      @@samyam Thank you so much, you are my hero of the day!

  • @DonDisainer
    @DonDisainer 8 месяцев назад

    dammm this tutorials are the best, hope you dont leave unity :((

  • @andrelima.2190
    @andrelima.2190 Год назад

    Does it work with touch too?

  • @afrakes4510
    @afrakes4510 Год назад

    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!

  • @dezikso9358
    @dezikso9358 5 месяцев назад

    I have this problem where every time I click on an object it moves further away from the camera

  • @dashromero6615
    @dashromero6615 2 года назад +1

    Quick question, but how could I get this working on touchscreens?

    • @samyam
      @samyam  2 года назад +1

      You can replace the mouse coordinates with your finger coordinates! I have a video here:
      ruclips.net/video/ERAN5KBy2Gs/видео.html

    • @user-cv1nx8xt8s
      @user-cv1nx8xt8s 2 года назад

      ​@@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.

  • @freestylerak6655
    @freestylerak6655 Год назад

    Does this work in unity 2d?

  • @ArtRiku
    @ArtRiku 5 месяцев назад

    Just so you know, this doesn't seem to work with 2D skined characters/object. I don't know why.

  • @kyleme9697
    @kyleme9697 2 года назад

    Wow do you ever pack a lot of material into 20 minutes.

  • @derekdipietro9805
    @derekdipietro9805 Год назад

    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!

    • @samyam
      @samyam  Год назад +1

      Thanks! Yes it’s me, probably was at my Naruto phase 🤣

    • @derekdipietro9805
      @derekdipietro9805 Год назад

      @@samyam Nothing wrong with a Naruto phase, it's a great little story. You rock!

  • @darkdoom907
    @darkdoom907 2 года назад

    Will this work with touch Screen?

  • @sameuntaekkim1493
    @sameuntaekkim1493 2 года назад

    21:47 thats me beaches

    • @samyam
      @samyam  2 года назад

      Hehe thanks for the support!!

  • @allengrayeg.tuates1071
    @allengrayeg.tuates1071 2 года назад

    HOW TO RUN THIS IN ANDROID

    • @samyam
      @samyam  2 года назад

      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

  • @oingomoingo7411
    @oingomoingo7411 4 месяца назад

    typed way to fast but other than that its good

  • @andreyradostny
    @andreyradostny 23 дня назад

    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 :(

    • @andreyradostny
      @andreyradostny 23 дня назад

      Oh, God, I added Binding to the MouseClick action while PlayMode was running, so it wasn't saved