Scale slider min to max value based on parent object scale descending
So, I'm real new to scripting and I'm lost. I've got a slider which counts up from -500 to 1000 of which I can set the time using mainSlider.value += 5.0f; which is great but I need it to be speed dependent on how quickly it's parent is scaling down in size.
I have the scaling down object working how I like having a minSize, scaleRate, and Current scale (scale). Here's the code for that.
public float minSize;
public float scaleRate;
public float scale;
private void Update()
{
transform.localScale = Vector3.one * scale;
scale -= scaleRate * Time.deltaTime;
if (scale < minSize) Destroy(gameObject);
}
and here's the code for the slider min to max over time using 5.0f as a place holder for now.
public Slider mainSlider;
public int minValue;
public int maxValue;
private void Start()
{
mainSlider.value = minValue;
}
private void Update()
{
if (mainSlider.value <= maxValue)
{
mainSlider.value += 5.0f;
}
}
All I need is for the +=5.0f to be not set solid like that but be at the minValue when the parent object is at it's start size and the maxValue to be reached when the minSize of the parent object is reached.
I've been playing around with mathf.lerp where the 0.5f is but I can't figure out how to get the t to scale the slider value properly, if I call the scale from the parent object it just forces the value to 0.
any thoughts??? First time I've gotten properly stuck.
You can set the $$anonymous$$/max value of a slider in the Inspector or by code:
https://docs.unity3d.com/ScriptReference/UI.Slider-$$anonymous$$Value.html https://docs.unity3d.com/ScriptReference/UI.Slider-maxValue.html
this will clamp the value within those boundaries.
I have set the $$anonymous$$ and max values and have made them increase from $$anonymous$$ to max with a scalespeed variable already, what I need is for the slider to be directly linked to it's parent objects scale transform, so when I slide the slider it changes the size of the objects scale, hope this makes more sense?
Answer by fafase · Mar 31, 2019 at 06:59 PM
what I need is for the slider to be directly linked to it's parent objects scale transform, so when I slide the slider it changes the size of the objects scale,
Based on that comment, you simply want to have your scale to listen to the changes of the slider.
https://docs.unity3d.com/ScriptReference/UI.Slider-onValueChanged.html
public Slider slider;
void Start()
{
mainSlider.onValueChanged.AddListener(SliderChanged);
}
private void SliderChanged(float value)
{
transform.localScale = new Vector3(value, value,value);
}
This is simple and I'll leave it to you to set the scale properly. But the concept is to use the event from slider so you know when it is changed.
Your answer
Follow this Question
Related Questions
Content Size Fitter should scale only in one direction 0 Answers
Stop counting score when Game over : don't know how to do it... 0 Answers
Problem with Slider 1 Answer
Player Prefab scale automatically goes to 1 x 1 x 1 whereas I have set it to 0.48 x 0.48 x 0.48 1 Answer
Built game on Android has GUI text pink, and slider menu has disappeared. 0 Answers