Search Unity

  1. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice
  2. Unity is excited to announce that we will be collaborating with TheXPlace for a summer game jam from June 13 - June 19. Learn more.
    Dismiss Notice

Question Lever control system

Discussion in 'Scripting' started by Grivik, Jul 8, 2020.

  1. Grivik

    Grivik

    Joined:
    Apr 22, 2020
    Posts:
    5
    I'm trying to make a lever that can be dragged into different positions, and depending on which position it's in, will increase or decrease the throttle of a large vehicle. How would I go about doing this?
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    39,079
    Step 1. Select your favorite search engine. I chose Google.

    Step 2. Type the subject of this post in along with "unity" appended.

    Step 3. ???

    Step 4. PROFIT!

    Screen Shot 2020-07-08 at 1.55.02 PM.png
     
  3. Grivik

    Grivik

    Joined:
    Apr 22, 2020
    Posts:
    5
    I did that. all those tutorials are for On/Off levers. I need one that you can position to any point and it will give off a different thrust value. Believe me, I search all over the net before asking questions here but thank you for the sarcasm.
     
  4. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    39,079
    You're welcome! It just seemed unlikely that it couldn't be found on the net, and I'm actually sure it's out there somewhere still, as there are quite a few racing games that are throttled.

    In any case, I offer you this little "hovering" demo game with a touchscreen analog throttle, works fine on mobile touch targets too:

    http://www.plbm.com/hovering/index.html

    The key takeaway for you is the 2D analog control, in this case a 2-axis one that combines throttle with steer, but there's no reason you can't just put two of them onscreen and have one only pay attention to throttle up/down, and have the other pay attention to steer left right.

    The actual finger-sensing (or mouse sensing) script is called VAButton. The scenes set the single VAButton up to take the entire width of the lower screen. To split throttle from steering, make two VAButtons, one on the left, one on the right. You would give them separate rectangles.

    I actually use that VAButton code in all my games on mobile. There's an equivalent in the Unity Standard Assets but I always found it multiple times harder to set up quickly, so I made my own.

    Full source and project located here, your choice of git host:

    Hovering is presently hosted at these locations:

    https://bitbucket.org/kurtdekker/hovering

    https://github.com/kurtdekker/hovering