如何在Godot中将精灵设置为特定帧

huangapple go评论86阅读模式
英文:

How to set a Sprite to a specific Frame in Godot

问题

我让玩家移动,当他通过实例化进入新的房间时,他的精灵显示他面朝默认方向(在我的情况下是向下)。因此,如果您从任何其他方向进入房间,看起来会很奇怪,因为在短暂的瞬间,即使您是从右边进入的,也可以看到玩家面朝下。我该如何告诉Godot在代码中将玩家精灵设置为特定的帧,以便我可以为每个方向设置适当的帧。我对Godot还不熟悉,我使用了HeartBeast的动作RPG教程来进行移动。因此,它使用了AnimationTree和AnimationPlayer。我尝试过"set_frame",但Godot只是说它不知道这个方法。

英文:

I have the Player move around and when he enters a new Room (via Instancing) his Sprite shows him facing in the Default direction (in my Case down). So If you enter a Room from any other direction then it looks weird, cause for a short Moment you can see the Player facing down even if you came from the right. How can I tell Godot to set the Player Sprite to a specific Frame in Code, so I can set it to the proper Frame for each Direction. I'm new to Godot and I used HeartBeast Action RPG Tutorial for my Movement. So it's using an AnimationTree and AnimationPlayer. I tried "set_frame" but Godot just says it doesn't know the Method.

答案1

得分: 0

如果你正在按照我认为你正在按照的教程系列(Godot动作RPG)进行操作... 你正在使用一个AnimationTreeAnimationNodeBlendSpace2DBlendSpace2D)。

BlendSpace2D根据输入向量"blend_position"来选择动画。这样,你可以使用BlendSpace2D根据运动方向或玩家角色的朝向来选择动画。例如,你可以有"idle_up"、"idle_down"、"idle_left"和"idle_right"动画,然后使用BlendSpace2D根据方向向量在运行时选择其中一个。

因此,你需要像这样设置BlendSpace2D的"blend_position":

animationTree.set("parameters/NameOfTheBlendSpace2D/blend_position", vector)

其中:

  • animationTree是一个变量,设置为AnimationTree
  • "NameOfTheBlendSpace2D"是你想设置的BlendSpace2D的名称(例如"Idle")。
  • vector是一个具有所需方向的Vector2D(例如Vector2.UP)。

这在教程系列的第6集中有示例(在AnimationTree中的所有方向上进行动画


你可以在HeartBeast的参考项目arpg-reference中找到一个参考项目,在那里你可以找到一个类似这样的函数update_animation_blend_positions

func update_animation_blend_positions():
	animationTree.set("parameters/Idle/blend_position", input_vector)
	animationTree.set("parameters/Run/blend_position", input_vector)
	animationTree.set("parameters/Attack/blend_position", input_vector)
	animationTree.set("parameters/Roll/blend_position", input_vector)

这里的"Idle"、"Run"、"Attack"和"Roll"都是BlendSpace2D,每个都配置了相应动作的动画,这个函数以同步方式更新它们,以便它们选择正确的动画。

据我所知,来自存储库的代码已经从教程系列中进一步重构。该存储库中的代码受MIT许可证保护。

英文:

If you are following the tutorial series I think you are following (Godot Action RPG)… You are using an AnimationTree with AnimationNodeBlendSpace2D (BlendSpace2D).

The BlendSpace2D picks an animation based on an input vector "blend_position". This way you can use BlendSpace2D to pick an animation based on the direction of motion or the direction the player character is looking at. For example, you can "idle_up", "idle_down", "idle_left", and "idle_right" animations, and use BlendSpace2D to pick one in runtime based on a direction vector.

Thus, you need to set the "blend_position" of the BlendSpace2D like this:

animationTree.set("parameters/NameOfTheBlendSpàce2D/blend_position", vector)

Where:

  • animationTree is a variable set to the AnimationTree.
  • "NameOfTheBlendSpàce2D" is the name of the BlendSpace2D you want to set (e.g. "Idle").
  • vector is a Vector2D with the direction you want (e.g. Vector2.UP).

This is shown in the episode 6 of the tutorial series (Animation in all directions with an AnimationTree).


You can find a reference project by HeartBeast at arpg-reference, where you can find a function update_animation_blend_positions that looks like this:

func update_animation_blend_positions():
	animationTree.set("parameters/Idle/blend_position", input_vector)
	animationTree.set("parameters/Run/blend_position", input_vector)
	animationTree.set("parameters/Attack/blend_position", input_vector)
	animationTree.set("parameters/Roll/blend_position", input_vector)

Here "Idle", "Run", "Attack", and "Roll" are BlendSpace2D, each configured with animations for the corresponding actions, and this function updates them in sync so that they are picking the correct animation.

As far as I can tell the code from the repository is further refactored from what is show in the tutorial series. This code from the repository is under MIT licence.

huangapple
  • 本文由 发表于 2023年1月9日 07:19:46
  • 转载请务必保留本文链接:https://go.coder-hub.com/75051980.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定