For example my health number is 50.
and i need to use slider to add or minus the health.
but my edit slider number is 0 - 15
so how can i add or minus the health number through slider number?
when i slider right (0 to 15) , health number add the slider end number. if i slide to 10 is 50 + 10
and slider left (15 to 0),health number return, and 0 is no change.
Assuming your health number ranges from 0-100, you can use the following formula to map the slider value sliderVal to the health value healthVal:
healthVal = (sliderVal / 15) * 100 // 100 is the max health
For example, if the slider value is 7, the corresponding health value would be:
healthVal = (7 / 15) * 100 = 46.67
You can then use this calculated health value to add or subtract from your current health number, depending on the direction of the slider.
For example, if the slider is used to increase health, you can add the calculated health value to your current health number:
newHealthNum = currentHealthNum + healthVal
And if the slider is used to decrease health, you can subtract the calculated health value from your current health number:
newHealthNum = currentHealthNum - healthVal
Note that you may need to round the calculated health value to an integer if your health number is an integer value.
Here is a more detailed example:
public Slider healthSlider;
private float previousSliderValue;
private void Update()
{
UpdateHealth();
}
public void UpdateHealth() {
float currentSliderValue = healthSlider.value;
// Determine direction of slider
float sliderDirection = currentSliderValue - previousSliderValue;
if (sliderDirection > 0) {
// Slider is moving in positive direction (adding health)
float healthToAdd = (sliderDirection / 15) * 100;
AddHealth((int)healthToAdd); // Assuming health is an
integer value
} else if (sliderDirection < 0) {
// Slider is moving in negative direction (subtracting
health)
float healthToSubtract = (-sliderDirection / 15) * 100;
SubtractHealth((int)healthToSubtract);
}
// Store current slider value for next update
previousSliderValue = currentSliderValue;
}
public void AddHealth(int healthToAdd) {
// Add health to current health number
// ...
}
public void SubtractHealth(int healthToSubtract) {
// Subtract health from current health number
// ...
}
Related
I'm trying to build some training app for functions for school. The problem is:
Everytime the randomly picked number is lower than 0, my function shows +-, because
I have a fixed format for my function.
EXAMPLE
I tried to use the NumberFormat of the Intl-Package, but then I can't use the int-values correctly. Is there a way to show a plus sign for positive numbers, while they are still usable to work with them?
Code so far:
int randomNumberMinMax(int min, int max){
int randomminmax = min + Random().nextInt(max - min);
if(randomminmax==0){
randomminmax = min + Random().nextInt(max - min);
}
//generate random number within minimum and maximum value
return randomminmax;
}
int a = randomNumberMinMax(-5, 5);
int b = randomNumberMinMax(-10, 10);
int c = randomNumberMinMax(-10, 10);
String task = "f(x) = $a(x+$b)²+ $c";
You could only show the plus when the number is positive like this for example
String task = "f(x) = $a(x${b >= 0 ? "+" : ""}$b)²${c >= 0 ? "+" : ""} $c";
I am trying to create a slider where the value gets changed exponentially.
Let’s say the minimum is 0 and the maximum is 100. The first half of the slider should change the value slowly, like 0-10 and afterwards faster. I looked up different sites on StackOverflow but none really made sense to me. There seems to be math operations like pow() and exp(). Best would be to put it all in one function so i can use it for different parameters with a function that looks like this:
function(slidervalue, min, max, factor)
and returns the value.
i have managed to create these 2 functions to solve my problem:
func setParamLog(sliderValue: Float, min: Float, max: Float)->Float
{
// Output will be between minv and maxv
var minv :Float = log(min);
var maxv :Float = log(max);
// Adjustment factor
var scale :Float = (maxv - minv) / (max - min);
return exp(minv + (scale * (sliderValue - min)));
}
func setSliderLog(wpm: Float, min: Float, max: Float)->Float
{
// Output will be between minv and maxv
var minv :Float = log(min);
var maxv :Float = log(max);
// Adjustment factor
var scale :Float = (maxv - minv) / (max - min);
return (((log(wpm) - minv) / scale) + min);
}
it would be great to figure out now how i can adjust the scale factor
You could create a custom UISlider that wraps the functionality you want.
import Foundation
class ExponentialSlider {
...
var exponentialValue: Double {
get {
return exp(Double(self.value))
}
}
}
And then call it directly from the slider like this:
yourSlider.exponentialValue
You could also use the min and max values of the slider to modify the result of the computed property.
what i come up with yet is this function:
func exponentSliderValue(slidervalue: Float, exp: Float, min: Float, max: Float)->Float
{
var diff: Float = max - min
var result: Float = pow(slidervalue / diff, exp)
var endresult: Float = ((result * diff) + min) * 10
endresult.round()
endresult = endresult / 10
if(endresult >= min)
{
endresult = endresult + 0
}
if(endresult < min)
{
endresult = min
}
if(endresult >= max)
{
endresult = max
}
return endresult
}
it works pretty good. what i havent figured out yet is to do the whole thing in the opposite direction. all my sliders have default values.. so.. when i have a value of 40. it should translate to the right position (which is 1000) on the slider by default.. i have no idea yet how to do that. it would be great if someone can give me a hint. thanks
In code below, I want to manage the amount of distance to travel when a left arrow key is pressed depending upon if it's half way down or not.
The object moves all the way to left on very first press instead of movement to be divided in 3 or 4 parts depending on the above mentioned condition, where am I doing it wrong?
var diff = Mathf.Abs(this.transform.position.x - r.renderer.bounds.min.x);
print("diff" + diff);
var lessdistancetotravel = diff/4;
var moredistancetotravel = diff/3;
if(this.transform.position.x > half)
{
print ("greater than half while moving left");
print("currentpos" + this.transform.position.x); //gives 0.6
print("moredistance " + moredistancetotravel);//gives 0.69
this.transform.position = new Vector3 (this.transform.position.x - moredistancetotravel,
this.transform.position.y,
this.transform.position.z);
print("updated" + (this.transform.position.x - moredistancetotravel)); //gives -0.78,How come?
}
Since you can't check how far down a key is pressed, as Jerdak mentioned in the comments. I would then just measure how long a key has been pressed. You can start counting how long the key has been down and stop counting once it is released. Then you can use that time to determine how far your object can travel.
How to count the time the key has been pressed:
float count = 0.0f;
void Update()
{
if(Input.GetKey("a"))
count += Time.deltaTime;
else if(Input.GetKeyUp("a"))
count = 0.0f;
}
Code resets count back to 0 once you release the key.
I have two UISlider called slider1
slider1.minimumValue = 0; slider1.maximumValue = 100;
i want to set a break point,such as 60, if the slider1 move to 60 from 0(left) to 60(right), it will stop here the thumb can not move to right, but it can move to left. so how can i do?
please take a look the following code, it doesn't work, thanks
-(IBAction)s1valuechanged:(id)sender{
if ((int)slider1.value > 60) {
slider1.userInteractionEnabled = FALSE;
}
else{
slider1.userInteractionEnabled =TRUE;
}
}
I'm having a bit of trouble understanding your question, so I'm going to assume that your slider looks like the following, and you want to prevent the user from moving the slider to a value greater than 60:
0 ----------60------100
|---valid----|-invalid-|
All you would need is the following:
-(IBAction)s1valuechanged:(id)sender{
if ((int)slider1.value > 60) {
slider.value = 60;
}
}
In other words, whenever the user tries to move the slider to a value greater than 60, move the slider back to 60.
// stampDuty percentages
float stamp1 = (invalue * 1 / PCENT);
float stamp2 = (invalue * 2 / PCENT); // #defined PCENT as 100
// stamp duty
NSNumber *stampN1 = [NSNumber numberWithFloat:stamp1];
NSNumber *stampN2 = [NSNumber numberWithFloat:stamp2];
// then goes to
// Stamp Duty invalue is entered in the textfield by user
if (invalue <= 125000.0f) {
NSLog(#"Equal to or under 175,000 1% Stamp Duty");
//stampN0
[lblIntrest setText:[NSString stringWithFormat:#"Stamp Duty: %#", [currencyStyle stringFromNumber:stampN1]]];
// need a common variable variable is picked up here
// the value that is calculated here is used with other totals to create a grand final total
}
if (invalue >= 125001.0f && invalue <= 250000.0f) {
NSLog(#"Greater than 125,000 and less than 250,000 StampDuty 1%%");
[lblIntrest setText:[NSString stringWithFormat:#"Stamp Duty: %#",[currencyStyle stringFromNumber:stampN2]]];
// need a common variable is picked up here
}
// returns with the appropriate value
//the value that is calculated here is used with other totals to create a grand final total.
// eg float Beven = (invalue + SaleP + stamp1 etc etc
If I use stamp1 in the above calculation it works fine. What I'm looking for is a common variable to enter into this calculation variable "string". There are other if statements.
Hope you can help
I suggest the variable name actualStampValue.
Also note, your code contains two errors:
Your first if checks that the number is <=125000, but the string it prints says 175000.
Your second if checks that the number is >=125001, which leaves out all the values in between 125000 and 125001. This check should be >125000.