【问题标题】:Unity 3d show specific frame of animationunity 3d 显示特定帧动画
【发布时间】:2013-12-02 16:54:42
【问题描述】:

我正在为 2d 游戏尝试新的 unity 3d 选项。我正在尝试创建根据我所做的操作而变化的背景。因此,如果我按下按钮一,我将精灵一作为背景,如果二,我将精灵二。由于我有 32 个选项,我发现最好的方法是拥有一个 Animator,它会根据按钮单击来更改帧。所以我创建了动画师和动画。但问题是我无法设置时间来停止动画以显示所选帧。

我正在尝试这样:

Animator ani=background.GetComponent<Animator>();
ani.animation["field_back_anim"].time=0.5f;
ani.speed=0;

但它在第二行失败并出现该错误:

MissingComponentException: There is no 'Animation' attached to the "background" game object, but a script is trying to access it.
You probably need to add a Animation to the game object "background". Or your script needs to check if the component is attached before using it.

但是,如果我不编写代码,动画只会播放所有 16 帧。所以我想毕竟有动画。

在背景游戏对象上,我有 2 个组件,第一个是精灵渲染器,第二个是动画师。当我在 Animator 视图中打开 animator 时,我看到绿色矩形显示任何状态,黄色矩形显示“field_back_anim”。我不明白我做错了什么。

我还将排除执行以下操作的任何其他解决方案。

谢谢!

【问题讨论】:

  • 如何获取代码中的背景对象?
  • 我将它作为公共游戏对象添加到检查器中。

标签: animation unity3d


【解决方案1】:

animator 组件用于控制许多动画剪辑之间的过渡。如果您要在游戏对象上播放动画剪辑,动画组件是合适的,不是动画师。移除动画器并将动画组件添加到检查器中的背景游戏对象。如果您将动画属性设置为 field_back_anim,您的游戏对象将会很好地制作动画。操作代码应更改如下。

Animation ani = background.GetComponent<Animation>();
ani["field_back_anim"].time = 0.5f;
ani["field_back_anim"].speed = 0;

【讨论】:

  • 这是一个解决方案,但我很想知道我开始的方式是否可行。谢谢!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2021-06-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-05-15
  • 2017-07-17
  • 1970-01-01
相关资源
最近更新 更多