How to move an object's own Y position relative to a slider's Y position?
Essentially, I have a slider that you click and move along the Y axis between -4 and 0 positions. I also have an object that I want to move when you click and drag the slider, on the Y axis between -4 and 4 positions.
Can anyone tell me how to achieve this? I've tried multiplying/dividing the float variables and creating an offset but they don't work. All I can achieve is the object moving alongside the slider's y axis.
I want the object to move up and down the -4 and 4 Y coordinates relative to the slider's -4 and 0 coordinates.
This is the code I've been tinkering with (forgive me if its messy but its the result of trying different things):
Vector3 pos = transform.position;
pos.y = Mathf.Clamp(pos.y, -4f, 0f);
transform.position = pos;
/*units = (int)Mathf.Ceil(pos.y);
units = Mathf.Clamp(units, 0, 100);
pos.y = Mathf.RoundToInt(units);
print(units);*/
Vector3 pos2 = spaceship.transform.position;
pos2.y = Mathf.Clamp(pos.y, -4f, 4f);
spaceship.transform.position = pos2;
/*units2 = Mathf.Clamp(units2, 0, 100);
units2 = (int)Mathf.Ceil(pos2.y);
pos2.y = units2;
units2 = units;*/
pos2.y = pos.y;
I've tried creating an Int that means 0-100 is the same as between -4 and 0 Y coordinates, in an effort to replicate the same result with the object's -4 to 4 Y coordinates. I'm obviously in a confusing position right now, as I'm probably trying code now that just isn't going to work. Can anyone figure out my issue? Thanks in advance.
Answer by alankemp · Feb 10, 2017 at 03:34 PM
objectY = (sliderY + 2) * 2;
I would never have thought to write that in a million years. I guess this is where learning to code on your own has its limitations!
Thank you for your help. It works wonderfully.
Your answer
![](https://koobas.hobune.stream/wayback/20220612110802im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Int not getting rounded data 0 Answers
Adding Int's from different and the same script 1 Answer
How to make a number higher than Quintillion? (1000000000000000000) 1 Answer
How can I change the value of a Float smoothly? 2 Answers
float number displayed as an int? or at least on the health bar 0 Answers