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

A better way to scroll items using mouse / finger.

Discussion in 'Scripting' started by tpainton, Oct 2, 2014.

  1. tpainton

    tpainton

    Joined:
    Jun 4, 2014
    Posts:
    16
    Hi, I'm new to unity but love it.. I'll never program for mobile natively again...

    I am scrolling items down the screen using the following code I came up with..

    Code (CSharp):
    1. void Update () {
    2.  
    3.         //If running game in editor
    4.         #if UNITY_EDITOR
    5.  
    6.         //If mouse button 0 is down
    7.         if(Input.GetMouseButton(0))
    8.         {
    9.             //Cache mouse position
    10.             Vector2 mouseCache = Input.mousePosition;
    11.             if (mouseCache.x <= Screen.width) {
    12.                 if (Input.GetAxis("Mouse Y") > 0f) {
    13.                     transform.position = new Vector3(transform.position.x, transform.position.y + mouseCache.y * .000009f, 0);
    14.                 }
    15.                 if (Input.GetAxis("Mouse Y") < 0f) {
    16.                     transform.position = new Vector3(transform.position.x, transform.position.y - mouseCache.y * .000009f, 0);
    17.                 }
    18.             }
    19.         }
    20.  
    21.         #endif
    22. }
    This seems a bit crude.. having to multiply by .00009f to slow things down seems a bit odd.. There must be an easier way.. I'm also still trying to figure out how to make this work using a finger on mobile.

    Is this is best utilization? Can I easily convert to finger tap and drag on iOS and Android? Thanks a ton. This platform rocks.
     
  2. Suddoha

    Suddoha

    Joined:
    Nov 9, 2013
    Posts:
    2,824
    You probably have to use that very small multiplicator because you use the 'mouse position' (the touch position) for the translation of the objects. The mousePosition/touch position is in screen coordinates though.
    Which means, you'd be moving the objects by XXX units(per frame!) when your current y position is XXX (let's say 500).
    Short: don't use the touch position directly for the translation.
     
  3. JoeStrout

    JoeStrout

    Joined:
    Jan 14, 2011
    Posts:
    9,840
    Well, the other thing is that you're moving by a fixed amount per frame... possibly forgetting that Update() is called anywhere from 3 to 60 frames per second. So you never want to move a fixed amount per frame, both because it will probably be too fast, and because the speed will vary with the frame rate.

    Instead, pretty much any sort of movement you do in Update, FixedUpdate, or OnGUI should be multiplied by Time.deltaTime.

    HTH,
    - Joe
     
  4. Suddoha

    Suddoha

    Joined:
    Nov 9, 2013
    Posts:
    2,824
    Update and FixedUpdate, but not OnGui :)
     
  5. tpainton

    tpainton

    Joined:
    Jun 4, 2014
    Posts:
    16
    Well, this is where I'm stuck. :(