- Home /
How to check if slider is moving?
Hey, how to check if slider is moving? It should do this stuff when moving and then stop when not moving slider.
import UnityEngine.UI;
var obj = GameObject("");
var animName = obj.GetComponent.<Animation>();
var whatAnimation = "";
var animWrapMode = WrapMode.PingPong;
var SliderPos : float;
var animationSlider = GetComponent(UnityEngine.UI.Slider);
var movingSlider = false;
function Start () {
animationSlider.direction = Slider.Direction.LeftToRight;
animationSlider.minValue = 0;
animationSlider.maxValue = 1;
animName[whatAnimation].enabled = false;
animName[whatAnimation].weight = 1;
animName[whatAnimation].wrapMode = animWrapMode;
}
function Update() {
animName[whatAnimation].normalizedTime = SliderPos;
//movingSlider = false;
if(movingSlider)
{
animName[whatAnimation].enabled = true;
}
else
{
animName[whatAnimation].enabled = false;
}
}
function moveSlider(newPos : float) {
SliderPos = newPos;
}
This piece is where things should be changed
if(movingSlider)
{
animName[whatAnimation].enabled = true;
}
else
{
animName[whatAnimation].enabled = false;
}
I think you could try with animation.isPlaying()
http://docs.unity3d.com/ScriptReference/Animation.IsPlaying.html
Answer by Mmmpies · Mar 15, 2015 at 08:14 PM
Look at the slider in the Inspector. At the bottom is an OnValueChanged. Click on the + and in the slot that appears drag the gameObject with your script on it to that.
Add a public function to your script to set a bool to true and record the Time.time when it set to true as a float.
Then in Update if that bool is true play the animation and check if Time.time is greater than that float, if it is set the bool to false so the next frame the animation doesn't play.
Now back to OnValueChanged from the dropdown select YourScriptName -> YourPublicFunctionName.
Hope that makes sense! :¬)
Thanks for answer, Sorry I didn't understand this ".. and record the Time.time when it set to true as a float.
Then in Update if that bool is true play the animation and check if Time.time is greater than that float, if it is set the bool to false so the next frame the animation doesn't play."
import UnityEngine.UI;
var obj = GameObject("");
var animName = obj.GetComponent.<Animation>();
var whatAnimation = "";
var animWrap$$anonymous$$ode = Wrap$$anonymous$$ode.PingPong;
var SliderPos : float;
var animationSlider = GetComponent(UnityEngine.UI.Slider);
var movingSlider = false;
var slider$$anonymous$$oves= false;
function Start () {
animationSlider.direction = Slider.Direction.LeftToRight;
animationSlider.$$anonymous$$Value = 0;
animationSlider.maxValue = 1;
animName[whatAnimation].enabled = false;
animName[whatAnimation].weight = 1;
animName[whatAnimation].wrap$$anonymous$$ode = animWrap$$anonymous$$ode;
}
function Update() {
animName[whatAnimation].normalizedTime = SliderPos;
if(slider$$anonymous$$oves > moveSlider.newPos)
{
animName[whatAnimation].enabled = true;
}
else
{
animName[whatAnimation].enabled = false;
}
}
function moveSlider(newPos : float)
{
SliderPos = newPos;
}
function isSlider$$anonymous$$oving(slider$$anonymous$$oves : float)
{
slider$$anonymous$$oves = Time.time;
}
Caught me at a bad time as I dont have Unity available but can you clarify if you want the animation to play when the pointer moves the slider and is it a set animation just to show tge sliders moving or do you want to rotate an object with the slider for example?
Yes this script controls animation with slider that works just fine, but object shakes so it's better to stop animation playing somehow when not moving slider(?)
Well it depends how you want it to work. If you don't $$anonymous$$d the animation playing when the pointer is clicking the slider but not moving you could set up an event trigger for pointer up to stop the animation.
Also as I won't have Unity for a few hours any chance you could post a youtube video so I can see What's happening?
Your answer
Follow this Question
Related Questions
How to control animation with UI slider? 2 Answers
UI Slider, Save Values and Change Them 0 Answers
Dynamic Values in Animation Clip 0 Answers