How can I make a GUI button play a simple animation when pressed?
Say for instance that I have a camera and lights all paired to a gimble that I want to animate with 3 or more animations for each button press to change the camera perspective around and object?
How is this done with GUI buttons?
You can try something like this:
function OnGUI() {
if (GUI.Button(Rect(10,70,50,30),"Click")){
gameObject.Find("YourGameObject").animation.Play();
}
}
I have tried this over and over again and I just can not get my animations to play, the first animation plays all by itself but my GUI buttons are ONLY playing the sounds and NOT the animations no matter what I do… what am I missing here?
// JavaScript
var CameraGimble : GameObject;
function OnGUI () {
// Make a background box
GUI.Box (Rect (10,10,200,90), "Camera View Menu");
// Make the first button. If it is pressed, Camera Animation 01 is Played
if (GUI.Button (Rect (20,40,80,20), "Side View")) {
//CameraGimble.animation.Play();
CameraGimble.animation.Play("CameraRotation01");
audio.PlayOneShot(audio.clip);
Debug.Log("Clicked Side View Button");
}
// Make the first button. If it is pressed, Camera Animation 02 is Played
if (GUI.Button (Rect (20,70,80,20), "Front View")) {
//animation.Play("CameraRotation02");
CameraGimble.animation.Play("CameraRotation02");
audio.PlayOneShot(audio.clip);
Debug.Log("Clicked Front View Button");
}
}
I had a similar problem with the animator component. I don’t know if this is the correct practice, but you can try this:
function OnGUI() {
if (GUI.Button(Rect(10,70,50,30),"Click")){
gameObject.Find("YourGameObject").GetComponent<Animator>().enabled = true;
}
}
By default (or on awake/start) let the animator component of the gameobject be disabled.