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

SOLVED = Sliders producing a math squaring effect

Discussion in 'Scripting' started by Genkidevelopment, Jan 23, 2015.

  1. Genkidevelopment

    Genkidevelopment

    Joined:
    Jan 2, 2015
    Posts:
    186
    I have GUI sliders controlling variables in an equation..

    However the equation is such that moving the slider a lot when its close to minimum has almost no effect, whilst moving the slider a little whilst it's close to maximum has a drastic effect!

    I see it as an 'x squared' problem... Is there a way to alter the way in which a slider decides it's output?

    Or will I have to compile a 'square root script' to separately 'smooth out' the way in which the sliders numerical output is percieved?

    Many thanks.

    Peace.
     
  2. N1warhead

    N1warhead

    Joined:
    Mar 12, 2014
    Posts:
    3,884
    This is the wrong section I believe,
    but some code would be nice.

    That's the only way we can really be of any help.
     
  3. Genkidevelopment

    Genkidevelopment

    Joined:
    Jan 2, 2015
    Posts:
    186
    Ah I see - sorry

    I don't think code is necessary, maybe I need to redefine the question...

    imagine clicking the slider one pixel to the right... then another... then another... the output value would increase by the same amount each time, yes? What I want is for the slider to 'add on' less output with each pixel to the right it is clicked...

    I have had a good look at the sliders options, I don't think what I need is available, thus I shall code it instead...
     
  4. N1warhead

    N1warhead

    Joined:
    Mar 12, 2014
    Posts:
    3,884
    What exactly do you mean by "Add On Less Output"?
    Like make the slider park move and the bar move closer to the slider?

    Like

    >-----------<

    have > moving right and < moving left to meet it?
     
  5. sphericPrawn

    sphericPrawn

    Joined:
    Oct 12, 2013
    Posts:
    244
    I'm also confused. So you want the slider to increment less and less the closer it gets to the max or the min? Kind of like a bell curve?
     
    Genkidevelopment and N1warhead like this.
  6. N1warhead

    N1warhead

    Joined:
    Mar 12, 2014
    Posts:
    3,884
    I just don't get why he can't show us the code, probably isn't any code and wants it given to him.

    I'm sure you agree, code and a clear explanation is the only true way to help him.
    I'm not going to go write up a script and *hope* it's the correct way lol.
     
  7. Genkidevelopment

    Genkidevelopment

    Joined:
    Jan 2, 2015
    Posts:
    186
    Yes :D

    * Easily done with script, but wondered if the sliders had an in built feature to set this...


    Actually not so sure it is! But I will look into it tomorrow...
    and what do I know? I still cant access and change the color component of a GUI image


    :D :DMight ask for that soon :p
     
    Last edited: Jan 23, 2015
  8. Genkidevelopment

    Genkidevelopment

    Joined:
    Jan 2, 2015
    Posts:
    186
    No offence, but I never asked for code - and I'm not shy in doing so if I need to...

    But again, thanks for trying to help me

    Peace
     
  9. N1warhead

    N1warhead

    Joined:
    Mar 12, 2014
    Posts:
    3,884
    Well how are we supposed to help if we don't even understand what you're trying to do,
    and won't even show us what you've done so we can piece it together and just add what needs to be added
    for it to work?
     
  10. Trexug

    Trexug

    Joined:
    Dec 2, 2013
    Posts:
    88
    I think I understand the challenge - sorry if this is way off, though.
    By using a gui slider the value of x is changed in the range [0..1]. x is being used in some equation where it is squared or cubed or something like that, which generates the value the user is interested in (let's call that x'). From the user's perspective it becomes difficult to select the desired x'-value for high values of x, as the granularity changes.

    I see two options which may be of use to you.

    Option 1. Transform with an easing function x before using it in the equation.
    Using an easing function will transform x, such that for x = 1 the transformed x will be 1 and for x = 0 the transformed x will be 0. That way, the user will still be able to manipulate the full range of values. There are some suggestions for easing functions here. You will probably want an easing function which eases out like easeOutCubic.

    Option 2. Let the slider determine the value of x' instead of x.
    Right now the user is changing a value, which affects the resulting value. It may be applicable to have the user change the resulting value instead. If you for some reason still needs the x-value, you can likely work your way backwards by isolating x from the equation, given x'
     
  11. Genkidevelopment

    Genkidevelopment

    Joined:
    Jan 2, 2015
    Posts:
    186

    Firstly, thank you for your detailed response... Indeed you are correct with understanding my situation, but my extended problem with this is... This slider, represents one part of an equation that also has another 4 sliders AND the x,y position of two other game objects involved...

    As I say... it's a long equation!...

    The slider does what it needs to perfectly... aside this squaring effect causing massive changes with the slightest slider movement.. The current slider output, for example between 0 and 1 is linear... As in, if the slider is set to 25%, it will output 0.25... 50% = 0.5... 75%= 0.75 etc.... What I am looking for is something to alter that linear output...

    Example... 25% = 0.5.... 50 = 0.8.... 75% =0.95 etc...

    I think these examples give a much better visualisation of what I require....

    Now about to check out the link that you advised...

    Thanks

    Peace
     
  12. Genkidevelopment

    Genkidevelopment

    Joined:
    Jan 2, 2015
    Posts:
    186
    I believe it can be scripted... where 'Slideroutput' is within my equation... I can substitute it for 'SmoothedSliderOutput!'

    SmoothedSliderOutput (SSO) = SliderOutput =(SO)

    Idea example... If
    trying square root...

    INTENDED SO range = 0 - 9
    9x9 = 81... so ACTUAL slider range 0 - 81....

    ...SSO = square root (sqr)of SO...

    SSO @ empty = sqr of 0 = 0
    SSO @ 25% = sqr of ((81/100)*25) = 4.5
    SSO @ 50% = sqr of ((81/100)*50) = 6.3
    SSO @75% = sqr of ((81/100)*75) = 7.8
    SSO @100% = sqr of ((81/100)*100) = 9

    See ^^ it's working... now Actual slider at 50% is not 4.5..... its 6.3 :D :D :D

    In my mathematical hey day... I'm sure there is a way to formulate this an find the EXACT square root I need... But I can try some guesses - play some 'higher'.... 'lower'... if you like!!

    Haha :D



    Errr... can we script square roots in C#!? Obviously * - + / but square roots?

    I guess we could use this equation...

    SSO = SO/SSO where SSO is an unknown variable... I think that will work :D I think that will work out the square root od SO that way!
     
    Last edited: Jan 23, 2015
  13. Genkidevelopment

    Genkidevelopment

    Joined:
    Jan 2, 2015
    Posts:
    186
    Code (csharp):
    1. public float UnsmoothedNewLine;
    2. public float NewHLine;
    3. public float HLine // <The one that finally makes it's way into the equation!
    4.  
    5. public void AdjustHLine (float NewHLine)
    6. {
    7. UnSmoothedNewLine = NewHLine;
    8. HLine = NewHLine * NewHLine;
    9. }
    10.  
    Although the principle here and the code works... It only serves to worsen the effect already created... I need the opposite to square root... I need to square it :D fun and games!!
     
  14. Genkidevelopment

    Genkidevelopment

    Joined:
    Jan 2, 2015
    Posts:
    186
    Oh no! - I am wrong... The above code works perfectly... IF we scale the actual slider range

    0 to 3.... thus result is 0 to 9... What I had done was squaring, by means of defining the actual slider the other way :D

    Problem SOLVED :D