Multiple Sliders act as one.
I've been struggling with this for several hours now. Making a ship combat sci fi game and I want to implement a system where the player can transfer power between weapons, engines and shields. I've set this up with 3 sliders, each represents one system. if I were to increase shield power by 10% both weapons and engine power should drop by 5%.
I've been trying to accomplish this with the onValueChanged function. I calculate the amount that has been changed and whether it is positive or negative by storing the startvalue and subtracting the new value. Then onValueChanged calls a function that subtracts the changedvalues to the other systems but its not working properly and its becoming really messy with too many nested if statements. i think the problem is how unity is dealing with the rounding as the value is only every changing by one number at a time and unity is subtracting 0.5 from the other system and rounding the results but quite frankly the method I am using is not efficient.
Has anyone tried to build a similar system and has any advice on how I should approach this problem?
Sorry to bother, but I'm trying to do something similar. Were you able to find a solution?
Answer by hexagonius · Feb 09, 2016 at 09:31 PM
I think you don't need to calculate the difference from the slider. OnValueChanged you know the current value of the slider (.6?) and the total of 100% (1) AND that there is two more sliders. so
1f - .6f = .4f
.4f / 2 (sliders) = .2f
that's the new value for both of the others. do this calculation on any if the sliders change and apply the result to the other two. since this is always starting from 1, there is no loss in precision
Brilliant! Thank you, I don't know why I've been having so much trouble wrapping my head around this. I'm at work now when I get home I'll play with this approach but it certainly seems like it will work. Thanks for the response ! :)
So, I ran into a couple issues with this method, the biggest issue is it doesn't allow me to have the sliders set at 3 separate values. i.e if I were to put the Shields at 70%, the weapons and engines would automatically put to 15% each. I couldn't set shields to 70, weapons to 20 and engines to 10. The other issue is if I put the onValueChanged attached to each of these sliders it essentially creates an infinite loop as the it is constantly evaluating all three slider, but I figure I can fix this problem by using a function like onDrag ins$$anonymous$$d of onValueChanged.
Any advice?