如何管理动画?

时间:2013-02-15 12:00:53

标签: c# animation unity3d

我正在使用C#而我正在构建第三人称视角游戏。我有一个3D角色模型,帧中有动画,所以我必须按帧剪切动画

问题是我目前有5个动画(空闲,跑步,走路,打卡,跳转),这是我的代码

void Update () {
    if (Input.GetAxis("Horizontal") != 0 || Input.GetAxis("Vertical") != 0){
        //play run animation
    } else {
        if (motion.x != 0 || motion.z != 0){
            motion.x = 0;
            motion.z = 0;
        }
                    //play idle anim
    }

    if (Input.GetKeyDown(KeyCode.Space) && controller.isGrounded){
    //play jump anim
    }

    if (Input.GetKeyDown(KeyCode.P)){
        //play punch anim
    }

    if (!controller.isGrounded){
        //play jump anim
    }

    vertVelo -= gravity * Time.deltaTime;
    motion.y = vertVelo;
    this.controller.Move(motion * Time.deltaTime);
}

按P键进行角色打卡时出现问题。似乎正在调用更新函数中的空闲动画,以便打卡动画没有时间播放。

那么,解决方案是什么?有没有动画管理技术或我应该使用延迟?

2 个答案:

答案 0 :(得分:1)

你可以在打拳时阻止空闲动画(但可能不是最好的方法):

bool isPunchPlaying = false;

void Update () {
    //... 
    if (!isPunchPlaying) {
        // Play idle anim
    }
    // ...
    if (Input.GetKeyDown(KeyCode.P)){
        //play punch anim
        isPunchPlaying = true;
        StartCoroutine(WaitThenDoPunch(animation["punch"].length));
    }
    // ...
}

IEnumerator WaitThenDoPunch(float time) {
    yield return new WaitForSeconds(time);
    isPunchPlaying = false;
}

答案 1 :(得分:0)

在Unity中尝试使用Google搜索动画图层并在动画之间进行混合。

您可以在具有较高图层编号的图层上设置动画循环,从而覆盖较低图层。

这意味着您可以在第1层使用空闲循环,并且这会持续播放。然后说你的跳转循环在第2层。当跳转逻辑触发跳转循环时,它会播放一次,然后空闲循环继续。

Unity中关于'AnimationState'的文档也很有用> link <

相关问题