- Home /
Jump to specyfic time/frame in current animation using animator?
Hey so pretty much I have one variable with time or frame. Let's say that my current animation is at time = 0 and after button is pressed it changes to time given in variable. How can it be achieved?
Answer by CupOfMayo · Oct 02, 2016 at 12:21 PM
Searching thru the web I actually figured it out by connecting few ideas :)
Animator anim;
public float normalizedTime = 0;
// Use this for initialization
void Start () {
anim = gameObject.GetComponent<Animator> ();
}
// Update is called once per frame
void Update () {
if (Input.GetKeyDown (KeyCode.Q)) {
jumpToTime (currentAnimationName (), normalizedTime);
}
}
void jumpToTime(string name, float nTime)
{
anim.Play (name, 0, nTime);
}
string currentAnimationName()
{
var currAnimName = "";
foreach (AnimationClip clip in anim.runtimeAnimatorController.animationClips) {
if (anim.GetCurrentAnimatorStateInfo (0).IsName (clip.name)) {
currAnimName = clip.name.ToString();
}
}
return currAnimName;
}
}
Answer by Enumerator_T · Jan 21, 2018 at 08:22 PM
Using events in the animation window allows me to check if the animation is playing by setting an animation event at the very start of the animation and triggering 'IntroPlaying' on the event its self to trigger the button state on the GUI.
public bool IntroStarted;
public void IntroPlaying()
{
IntroStarted = true;
}
If the Animation has started, display the button and set the time of the animation state called "RoomEnter" then set "IntroStarted" to false
public Camera Cam;
public Animator anim;
private void OnGUI()
{
if (IntroStarted)
{
if (GUI.Button(new Rect(Screen.width / 2 - 50, 10, 100, 50), "Skip"))
{
anim = Cam.gameObject.GetComponent<Animator>();
anim.Play("RoomEnter", 0, 10.0f);
IntroStarted = false;
}
}
}
Cam is the MainCamera Game Object which you will have to set either programatically or using public Camera Cam;
Hope this helps.