首页 文章

在实例化对象上播放/停止动画/动画制作

提问于
浏览
2

我正在开发一个统一游戏,基本上,我有一个带有精灵的预制件 . 我创建了一个附加到该精灵的动画 .

FrogPrefab
    |__ FrogSprite

我创建了一个带有公共字段“prefab”的脚本,在那里我通过了我的预制件 .

我的问题是,如何从我的脚本中停止并播放此动画 .

我从我的启动方法中实例化了我的预制件......

public GameObject gameCharacterPrefab;

private GameObject frog;

void start() {
    frog = (GameObject)Instantiate(gameCharacterPrefab, objectPoolPosition, Quaternion.identity);
}

我正在尝试做那样的事......

frog.animation.stop();

感谢任何帮助

1 回答

  • 2

    首先,请注意该函数应该被称为 Start 而不是 start . 也许这是问题中的拼写错误,但值得一提 .

    使用 GetComponent 获取 AnimatorAnimation 组件 . 如果动画是预制件的子节点,则使用 GetComponentInChildren .

    If using the Animator component:

    public GameObject gameCharacterPrefab;
    private GameObject frog;
    Vector3 objectPoolPosition = Vector3.zero;
    Animator anim;
    

    实例化预制件

    frog = (GameObject)Instantiate(gameCharacterPrefab, objectPoolPosition, Quaternion.identity);
    

    获取 Animator 组件

    anim = frog.GetComponent<Animator>();
    

    播放动画状态

    anim.Play("AnimStateName");
    

    停止动画

    anim.StopPlayback();
    


    If using the Animation component:

    public GameObject gameCharacterPrefab;
    private GameObject frog;
    Vector3 objectPoolPosition = Vector3.zero;
    Animation anim;
    

    实例化预制件

    frog = (GameObject)Instantiate(gameCharacterPrefab, objectPoolPosition, Quaternion.identity);
    

    获取 Animation 组件

    anim = frog.GetComponent<Animation>();
    

    播放动画名称

    anim.Play("AnimName");
    

    停止动画

    anim.Stop();
    

相关问题