首页 > 代码库 > unity动画相关

unity动画相关

当前动画控制器是在播放哪个动画:

Animator alice;
AnimatorStateInfo currentBaseStage;

int idle = Animator.StringToHash("Base Layer.Idle");
int hit = Animator.StringToHash("Base Layer.Jump");

currentBaseStage=alice.GetCurrentAnimatorStateInfo(0);

if(currentBaseStage.nameHash == hit){//hit动画中

}

//播放指定的动画

alice.SetTrigger("Jump");

//如果一个动画还没播完又要重新开始播,可以考虑的做法如下:

if (currentBaseStage.nameHash == idle && !alice.IsInTransition(0))
{
alice.SetBool("Jump", false);
isHit = false;

}
//hit
if (!isHit &&Input.anyKeyDow&& !alice.IsInTransition(0) && currentBaseStage.nameHash == idle)
{
alice.SetBool("Jump", true);
alice.StartRecording(0);//记录重播开始位置参数是帧数
isHit = true;
//rbody.AddForce (Vector3.up * 50);
}
if (isHit && Input.anyKeyDow && currentBaseStage.nameHash == hit)
{
//二次受击
isPlayBack = true;
alice.StopRecording();//停止记录重播
alice.StartPlayback();//开始重播
alice.playbackTime = 0f;//从0s开始重播
Debug.Log("hit");
}

//设置现在重播时播放的时间
if (isPlayBack)
{
float newPlaybackTime = alice.playbackTime + Time.deltaTime;
if (newPlaybackTime > alice.recorderStopTime) {
newPlaybackTime = 0;
alice.StopPlayback();
isPlayBack = false;
}
alice.playbackTime = newPlaybackTime;
}

unity动画相关