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.
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));
}
Figured this out the answer to my question and just thought I would share my code. Here is my version of how to control animation playback with a slider and play/pause buttons. Happy to answer any questions anyone might have.
private var hSliderValue : float = 0.0;
private var myAnimation : AnimationState;
private var sliderClick = false;
function Start ()
{
myAnimation = animation["Take 001"];
myAnimation.speed = 0;
}
function Update ()
{
//Assigns slider value to animation time
myAnimation.time = hSliderValue;
//Variable and conditional to assign boolean value to sliderClick
sliderClick = false;
if(Input.GetMouseButtonDown(0) == true || Input.GetMouseButtonDown(1) == true)
{
sliderClick = true;
}
}
function OnGUI ()
{
//Background for animation controls
GUI.Box (Rect(0, Screen.height - 40, Screen.width, 40),"");
// Uses animation speed check to show the either play or pause buttons
if (myAnimation.speed == 1)
{
if (GUI.Button (Rect (10, Screen.height - 30, 50, 20), "Stop")) //Pauses animation
{
myAnimation.speed = 0;
}
}
else
{
if (GUI.Button (Rect (10, Screen.height - 30, 50, 20), "Play")) //Plays animation
{
myAnimation.speed = 1;
}
}
//A click on the slider area will pause the animation
if (new Rect(70, Screen.height - 25, 1050, 10).Contains(Event.current.mousePosition) && sliderClick == true)
{
myAnimation.speed = 0;
}
//click and drag slider to control animation playback
hSliderValue = GUI.HorizontalSlider (Rect (70, Screen.height - 25, 1050, 10), myAnimation.time, 0.0, myAnimation.length);
//Stop animation when it ends
if(myAnimation.time >= 83.2)
{
myAnimation.speed = 0;
}
}
Thanks alot for this code. Helped a bunch. I converted it to a C# equivalent for the community to leverage.
Thanks again.
OJ
using UnityEngine;
using System.Collections;
public class AnimationController : MonoBehaviour
{
private float hSliderValue = 0;
private AnimationState myAnimation;
private bool sliderClick = false;
// Use this for initialization
void Start ()
{
myAnimation = animation["Take 001"];
myAnimation.speed=0;
}
// Update is called once per frame
void Update ()
{
//Assigns Slider value to animation time
myAnimation.time=hSliderValue;
//Varaiable and conditional to assign boolean value to sliderClick
sliderClick = false;
if(Input.GetMouseButtonDown (0)== true || Input.GetMouseButtonDown (1)==true)
{
sliderClick = true;
}
}
void OnGUI()
{
//Background for animation controls
GUI.Box (new Rect (10,Screen.height-40, 350,40),"");
// GUI.Box (new Rect (10, 20, 85, 25), "Period");
//Uses animation speed check to show the either play or pause buttons
if (myAnimation.speed ==1)
{
if (GUI.Button (new Rect (15,Screen.height-30,50,20),"Stop")) //Pauses Animation
{
myAnimation.speed=0.0f;
}
}
else
{
if (GUI.Button (new Rect (15, Screen.height-30,50,20),"Play")) //Plays animation
{
myAnimation.speed = 1;
}
}
//A click on the slider area will pause the animation
if (new Rect(70, Screen.height - 25, 275, 10).Contains(Event.current.mousePosition) && sliderClick == true)
{
myAnimation.speed = 0;
}
//click and drag slider to control animation playback
hSliderValue = (GUI.HorizontalSlider (new Rect (70, Screen.height-25,275,10), myAnimation.time,0.0f,myAnimation.length));
//Stop animation when it ends
if(myAnimation.time >= 83.2)
{
myAnimation.speed = 0;
}
}
}
Just put that C# script on any object which has an animation in it with name “Take 001”. And dont forget to change animation type to “Legacy” in RIG folder.
here is a simple code for C#
using UnityEngine;
using UnityEngine.UI;
using System.Collections;
public class AnimControl : MonoBehaviour {
Animation anim;
public Slider slider;
// Use this for initialization
void Start () {
anim = GetComponent<Animation> ();
anim.Play ("SphereAnim");
anim ["SphereAnim"].speed = 0;
}
// Update is called once per frame
void Update () {
anim["SphereAnim"].time = slider.value;
}
}
Hi @vfxjex
When the title “SphereAnim” changes to the title of my animation, the text turns red and is not recognized.
Already checked and it is spelled correctly. The text is “ArmarioAnim”.
Can someone help me?