Drag and Drop with New Input system! ( Touch and Mouse )

Поделиться
HTML-код
  • Опубликовано: 18 июн 2024
  • Download the code from here gist.github.com/Mohammad9760/...
    in this video you'll learn how to use the new input system to drag and drop game objects by dragging the mouse or on touch screen. we setup the input action bindings on the component so you don't have to setup anything, you can just download the code and attach it on objects and you'll be able to drag and drop them.
    Subscribe / @triplequotes
    - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
    How To Rotate Objects with New Input system • Rotate Objects with Ne...
    Unity Tutorial Playlist • Detect Double Clicks i...
    Unity VFX Tutorials • Realistic Muzzle Flash...
    - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
    time stamps
    0:00 intro
    0:55 set up
    1:28 Input Actions
    3:15 get touch / mouse position
    4:35 check if clicked on
    7:25 drag
    10:00 add physics
    11:10 demo

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

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

    THANK YOU!! Helped me out sooooo much!

  • @InhumanBean
    @InhumanBean 11 месяцев назад

    Very good tutorial, thank you!

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

    Very helpful and clear video
    thank you very much :))

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

    I like this method a lot. Is there a way to do this without having to put this script on your 'grabbable' objects? Also is there a reason you prefer to use properties instead of just calling the raycasts etc in your functions?

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

      Hi. there's definitely a way to do it without attaching a script to those objects but I found this to make sense and be more predictable.
      and yes when you do raycasts in a property the raycast runs only when you use the property instead of every frame if you do it in update for example and it's also cleaner like this I think, so that's why XD

  • @zerop6821
    @zerop6821 10 месяцев назад

    Can anyone help me?
    I want to adjust the settings of the game controls, it is for Android
    The settings are such that the party can move the fire button and customize it as he likes

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

    Can you write this code without using ternary operators? Im unsure why when you subscribe to the actions, like press.performed that you set it equal to _ , with a ternary operator afterwards, eg: press.performed += _ => (other code here)

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

      Hi Reid, performed is an event in InputAction objects and you can add method calls to an event (aka subscribing to it) and ( ) => is the syntax for anonymous functions but it you don't need any arguments passed to the anonymous function you can just put an _ there.
      so you can define a method and subscribe that to an event or you can subscribe an anonymous function to it.
      let me know if my explanation was not enough, cheers.

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

      @@GameDevGeeks Don't we need to unsubscribe using -= in an OnDisable method?

    • @GameDevGeeks
      @GameDevGeeks  9 месяцев назад +1

      @@Mehrdad995GTa it's generally a good practice to help GC but unless you want to destroy the object unsubscribing would'nt do much.

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

      @@GameDevGeeks Thanks for your help.🙏

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

    When i drag this object and want to drag another, how can i do to get rid of the first object?

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

      Hi👋 if both objects have colliders and the drag&drop script set up on them it should work with both of them.

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

    If you have any questions ask them here :)

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

      how to Drag the object fast and throw it far away ?

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

      @@supersocial9002 you may want to get the direction of camera to the object and add it to the objects velocity when its released but depends on what youre looking for

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

      Have you got a version for 2D without physics?

    • @mrquokka4733
      @mrquokka4733 Месяц назад

      Why does the drag method return Ienumerator?

    • @GameDevGeeks
      @GameDevGeeks  Месяц назад

      @@mrquokka4733 that is a Coroutine aka an async function, which means that this method will not block the execution and can run while other stuff is happening. you could instead have conditions in the Update method which runs every frame, but doing it with a coroutine is more performant and the code ends up cleaner.
      you can search for coroutines in C# to learn more about it.
      cheers :)

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

    iam trying it in yellow head cap , which is on table , but it is fall down to floor ? , how can i back it to table again ?

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

      you need to add colliders to all the objects that you don't want to go through eachother (if you add a MeshCollider make sure to make it convex)

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

      thanks a lot @@GameDevGeeks

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

      @@sameralkhalili6554 glad I can help