x


Using GUI slider to control animation on object

Can anyone supply a javascript example for controlling let's say a box with the GUI Slider function? And I mean baked animation from 3D Max is assigned to the box object but I need the animation to be controlled by the float values from the slider. Ex - Box is animated in 3D max and moves from 0,0,0 to 0,20,0. This is exported as FBX. Import Box with animation to Unity and then have horizontal GUI slider affect the animation of the box, whereas float 0 = 0,0,0 and float 10= 0,20,0. Hope that makes sense.

more ▼

asked Apr 19 '11 at 08:37 PM

Sean 14 gravatar image

Sean 14
1 1 1 1

(comments are locked)
10|3000 characters needed characters left

2 answers: sort voted first

Well, just take the example from the GUI slider reference and from Animation.Sample and you're done.

private var hSliderValue : float = 0.0;
private var myAnimation : AnimationState;

function Start(){
    myAnimation = animation["MyClip"];
}

function LateUpdate() {
    myAnimation.time = hSliderValue;
    myAnimation.enabled = true;

    animation.Sample();
    myAnimation.enabled = false;
}

function OnGUI() {
    hSliderValue = GUILayout.HorizontalSlider (hSliderValue, 0.0, myAnimation.length,GUILayout.Width(100.0f));
}
more ▼

answered Apr 19 '11 at 08:48 PM

Bunny83 gravatar image

Bunny83
46.9k 12 50 210

Excellent! Thanks for the quick response! I'm not a coder but learning.

Apr 19 '11 at 09:07 PM Sean 14
(comments are locked)
10|3000 characters needed characters left

Hi Bunny83

Thanks for the helpful gui slider example. I am trying take this script a bit further and being a novice both with Unity and scripting I'm having some problems figuring it out. I actually am not sure if what I'm trying to do is feasible

What I would like to do is have the slider control the playback of multiple AnimationClips

So, rather then specify a particular AnimationClip in the script I am trying to create and AnimationClip array and link the AnimationStates of the the various clips to the hSliderValue. I am trying to use this example

[http://docs.unity3d.com/Documentation/ScriptReference/Animation.html][1]

I tried amending your script like so

private var hSliderValue : float = 0.0; private var myAnimation : AnimationState; var anims : AnimationClip[];

function Start(){ // myAnimation = animation["Take 001"]; }

function LateUpdate() { for(var myAnimation : AnimationState in anims) {
myAnimation.time = hSliderValue; myAnimation.enabled = true;

    animation.Sample();
    myAnimation.enabled = false;
}    

}

function OnGUI() { GUILayout.BeginArea (Rect (420,25,510,40)); hSliderValue = GUILayout.HorizontalSlider (hSliderValue, 0.0, myAnimation.length,GUILayout.Width(500.0f)); GUILayout.EndArea (); }

Ideas?

Thanks!!

more ▼

answered Apr 03 at 03:40 PM

kamullis gravatar image

kamullis
1

(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x5275
x3935
x19

asked: Apr 19 '11 at 08:37 PM

Seen: 2491 times

Last Updated: Apr 03 at 03:40 PM