首页 文章

启用后如何使模型跳转到第一个动画帧?

提问于
浏览
0

Unity C#问题 .

我有一个带动画控制器的模型 . 在通过SetActive(false)禁用模型之前,我需要让它“跳转”到动画的第一帧 . 因此,如果我在动画期间禁用模型,然后再次启用它,模型应重置其动画并显示为“默认”状态,因此我可以从头开始动画 .

The problem is that if I disable and enable the model during the animation, it appears frozen in the same frame which it has when I disabled it.

我尝试添加从“任何状态”到默认状态的转换,将转换持续时间设置为0,并添加“reatart”bool parametr来触发转换 . 然后:

myObject.SetBool("restart", true);
myObject.SetActive(false);

但它不起作用,对象在动画中间再次出现冻结 .

我想也许它仍然需要一些时间来进行转换,所以我尝试使用coroutine作为解决方法:

  • 仅禁用网格渲染器
  • 在Animator中开始过渡到第一帧动画(过渡持续时间为0)
  • 等一会儿
  • SetActive(false)

但是当我再次启用它时,它仍然在动画中间被冻结 .
为什么会这样?它怎么能修复?
非常感谢你提前!

这是代码示例:

public void disableObject(){
    StartCoroutine(disableObjectCoroutine());
}

public void enableObject(){
    myObject.GetComponent<Renderer>().enabled = true;
    myObject.SetActive(true);
}

IEnumerator disableObjectCoroutine(){
    myObject.GetComponent<Renderer>().enabled = false;
    myObject.GetComponent<Animator> ().SetBool ("restart", true);
    yield return new WaitForSeconds(.1f);
    myObject.GetComponent<Animator> ().SetBool ("mainAnimation", false);
    myObject.SetActive(false);
}

1 回答

  • 0

    你可以尝试这样的想法:

    int layer = 0;
    int hashName = myAnimator.GetCurrentAnimatorStateInfo(0).shortNameHash; //get current animation name
    myAnimator.Play(hashName, layer, 0); // 0 means first frame
    myObject.SetActive(false);
    

相关问题