在Unity中,可以使用AnimatorStateInfo中的normalizedTime属性来设置动画的循环时间。normalizedTime是一个介于0和1之间的标准化时间,表示动画播放的进度。设置normalizedTime的值可以控制动画播放的位置。

以下是一个示例代码,在运行时动态设置动画的循环时间:

Animator animator = GetComponent<Animator>();
AnimatorStateInfo stateInfo = animator.GetCurrentAnimatorStateInfo(0);
float loopTime = 0.5f; // 设置循环时间为0.5秒

float normalizedTime = (stateInfo.normalizedTime % 1) * loopTime / stateInfo.length;
animator.Play(stateInfo.fullPathHash, -1, normalizedTime);

首先获取动画组件和当前状态信息。然后计算出所需的normalizedTime值,它是当前normalizedTime值的模1(因为normalizedTime是循环的),乘以所需的循环时间,再除以动画长度。最后,使用animator.Play()方法播放动画,传递所需的normalizedTime值


原文地址: https://www.cveoy.top/t/topic/cIP9 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录