Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Question How to add joystick in Unity UI Toolkit

Discussion in 'UI Toolkit' started by Mohak_dev, Sep 24, 2022.

  1. Mohak_dev

    Mohak_dev

    Joined:
    Sep 24, 2021
    Posts:
    7
  2. ZeroKuru

    ZeroKuru

    Joined:
    Jul 29, 2020
    Posts:
    3
    Hi there @Mohak_dev, did you manage to get this figured out? I'm contemplating making the switch to UI Toolkit myself for our upcoming mobile game since it's a lot better than the classic Canvas system because it can scale much better to fit different screens and such.

    I'd love to make a joystick myself and I haven't found much info online yet.

    Looking forward to hearing from you!
     
  3. uBenoitA

    uBenoitA

    Unity Technologies

    Joined:
    Apr 15, 2020
    Posts:
    198
    Hi Mohak_dev,

    I suggest you
    joystickElement.RegisterCallback<PointerDownEvent>(OnPointerDown)
    on your joystick element, then in OnPointerDown(PointerDownEvent e) you call
    joystickElement.CapturePointer(e.pointerId)
    . Do the same thing with PointerUpEvent but with
    ReleasePointer
    instead. Then register a callback to PointerMoveEvent where you will calculate the delta between the center of your joystick element (you can use
    joystickElement.worldBound.center
    ) and the pointer position. You will probably want to check if the delta is larger than a certain maximum and if so normalize it and multiply it by the maximum distance, to keep your input within some expected bounds.

    Then you do what you wanted to do with the joystick delta you extracted. You will probably want to store it in a local variable and use it in an Update loop, etc. Be sure to reset your delta to 0 in your PointerUpEvent callback, too.

    I haven't written my description in proper code but if you're used to UI Toolkit a bit, it's relatively standard stuff. I hope this gives you a reasonable starting point!
     
  4. erhardsc

    erhardsc

    Joined:
    Jun 10, 2020
    Posts:
    1
  5. theunsigned

    theunsigned

    Joined:
    Nov 15, 2018
    Posts:
    31
    This is great and it would be a great example for a Unity tutorial. Kindly go into detail because it seems the most efficient option is to use a hybrid UI system and that seem counterintuitive.
     
    Last edited: May 8, 2023
  6. uBenoitA

    uBenoitA

    Unity Technologies

    Joined:
    Apr 15, 2020
    Posts:
    198
    Hi theunsigned. I've attached a small project that implements what I had explained earlier. The art is extremely crude but you should get the right working principles. About making an official tutorial out of it, it's a good idea, I'll mention it to some colleagues and we'll see.
     

    Attached Files:

  7. restush96

    restush96

    Joined:
    May 28, 2019
    Posts:
    121
    @uBenoitA Have your colleagues created tutorials?