Search Unity

How to design a smooth up/down UI scroll inside Update() for a joystick input?

Discussion in 'Editor & General Support' started by AzzyDude24601, Nov 9, 2021.

  1. AzzyDude24601

    AzzyDude24601

    Joined:
    Sep 28, 2016
    Posts:
    51
    I am developing a VR application in Unity and I am struggling to develop a smooth UI scroll using my VR controller's joystick. So far what I have looks like this...

    Code (CSharp):
    1. private void Update()
    2. {
    3.     float joyStickDirection = globals.menuInteraction_Scroll.GetAxis(SteamVR_Input_Sources.Any).y; // this is either 1 for up or -1 for down
    4.     if (joyStickDirection != 0)
    5.     {
    6.         float multiplier = joyStickDirection * 5f;
    7.         scrollRect.verticalNormalizedPosition = scrollRect.verticalNormalizedPosition + (multiplier * Time.deltaTime);
    8.     }
    9. }
    ...this works, but has two problems. Firstly, it scrolls at different speeds depending how big the scrolling container is. Secondly, the scrolling is not very smooth, as it is clearly just skipping varying gaps between 0 and 1.

    I think I know what's wrong but I don't have enough experience working inside Update() to figure out the correct approach. Can anyone advise?
     
  2. AzzyDude24601

    AzzyDude24601

    Joined:
    Sep 28, 2016
    Posts:
    51
    I can also switch joyStickDirection to capture how far the joystick is pushed so it's any point from -1 to 1 instead of just -1 or 1.
     
  3. AzzyDude24601

    AzzyDude24601

    Joined:
    Sep 28, 2016
    Posts:
    51
    Settled for this strategy:

    Code (CSharp):
    1. public class ScrollExample : MonoBehaviour
    2. {
    3.     public float speed = 5f;
    4.  
    5.     public Transform ScrollContent;
    6.    
    7.     void Update()
    8.     {
    9.         // this is either 1 for up or -1 for down
    10.         var joyStickDirection = globals.menuInteraction_Scroll.GetAxis(SteamVR_Input_Sources.Any).y;
    11.         if (joyStickDirection != 0)
    12.         {
    13.             var multiplier = joyStickDirection * speed;
    14.            
    15.             // You want to invert the direction since scrolling down actually means
    16.             // moving the content up
    17.             ScrollContent.position -= Vector3.up * multiplier * Time.deltaTime;
    18.         }
    19.     }
    20. }