首页 > 其他分享 >动画的播放控制

动画的播放控制

时间:2023-03-11 15:13:43浏览次数:32  
标签:动画 控制 am KeyCode Controller Input 播放 id Animator

1.通过组件控制

导入模型后,在Project中新建一个Animator Controller,双击Animator Controller,将模型内部的动画文件拖入到Animator Controller的场景中,再将Animator Controller拖入到模型的Animator组件(如果没有该组件可以新创建一个)的Controller中,再点击运行模型即可动起来。

2.通过脚本控制

示例

using UnityEngine;

public class Test : MonoBehaviour

{

    Animator am;

    void Start()

    {

        am = transform.GetComponent<Animator>();

    }

    void Update()

    {

        if (Input.GetKeyDown(KeyCode.Alpha0))

        {

            am.SetInteger("id", 0);

        }

        else if (Input.GetKeyDown(KeyCode.Alpha1))

        {

            am.SetInteger("id", 1);

        }

        else if (Input.GetKeyDown(KeyCode.Alpha2))

        {

            am.SetInteger("id", 2);

        }

        else if (Input.GetKeyDown(KeyCode.Alpha3))

        {

            am.SetInteger("id", 3);

        }

        else if (Input.GetKeyDown(KeyCode.Alpha4))

        {

            am.SetInteger("id", 4);

        }

    }

}

在Prooject中创建一个Animator Controller,将动画拖进Animator Controller中,在Animator中设置通过Any State设置动画的播放,在Parameter中设置一个名叫id的变量,点击Animater中的箭头,在Inspector中设置动漫所对应的值,并将Inspector>Setting>Can Transition To Self取消勾选,通过该代码可以实现在运行时,通过按设置的值实现相应的动画效果。

标签:动画,控制,am,KeyCode,Controller,Input,播放,id,Animator
From: https://www.cnblogs.com/gatran/p/17206072.html

相关文章