Effect Maker教程:用子图与帧间隔实现游戏物品生成、移动与碰撞检测
Effect Maker Tutorial: Gaming 102 with @cloves_ar
针对独立开发者或想快速制作互动特效的用户,提供了从物理运动标准化到碰撞检测的完整代码级实现路径,参数具体可直接复用。
Hey, it's Clubs again and today we are building a catch game. Here's how the game works. The player needs to catch as many ice creams as possible with their mouth before the [music] timer ends. Along with the ice creams, broccoli will also fall from the sky. If the player eats the broccoli by accident, [music] they lose one life. Lose three lives and the game is over. Now that we know how the game works, let's break it down step by step.
嘿,又是 Clubs 来了。今天我们要制作一个接物游戏。游戏规则如下:玩家需要在 [音乐] 计时器结束前,用嘴巴尽可能多地接住冰淇淋。除了冰淇淋,西兰花也会从天而降。如果玩家不小心吃掉了西兰花,[音乐] 就会失去一条生命。失去三条生命后,游戏结束。既然我们已经了解了游戏的运作方式,接下来让我们一步步分解实现。
[music]
[音乐]
First, we'll build the logic for spawning items and make them fall at random horizontal positions. Next, [music] we'll add collision so these items can hit the mouth. After that, we'll build the scoring logic. And finally, we'll create the game's UI, reset logic, and extra screens like the start screen, game win screen, and game over screen. Let's get started. From the homepage, create a new project. Let's also add an image.
首先,我们将构建生成物品的逻辑,并使它们以随机的水平位置下落。接下来,[音乐] 我们会添加碰撞检测,让这些物品能够击中嘴巴。然后,我们将构建计分逻辑。最后,我们将创建游戏的用户界面、重置逻辑以及额外的屏幕,如开始屏幕、胜利屏幕和失败屏幕。让我们开始吧。从主页创建一个新项目。我们还需要添加一张图片。
Open the image panel. Click add image. Then let's upload an image just so it doesn't look empty. I'm going to choose one of the ice creams we will use later. Let's open the visual script and start building our logic. Our spawning in motion logic will be reused for several images. So to keep things scalable, we'll build it inside a subgraph. To do that, right click, then go to subgraphs and new subgraph. Before we open it, double click its name and rename it to position increment.
打开图像面板。点击“添加图像”。然后上传一张图片,以免画面看起来空荡荡的。我选择稍后要使用的一张冰淇淋图片。让我们打开可视化脚本并开始构建逻辑。我们的生成与运动逻辑将在多张图片中复用。为了保持可扩展性,我们将在子图中构建它。为此,右键单击,然后进入“子图”并选择“新建子图”。在打开之前,双击其名称并将其重命名为 position increment。
Let's open the subgraph and start building. We won't start seeing objects here. Instead, we will use variables. So, this logic works for any image later. Okay. to send and receive information from the main graph. We'll use this subgraphs inputs and outputs. If that is still a little confusing, don't worry. You will see exactly how it works as we go. Okay, let's start with continuous motion on the y-axis. Moving the image down.
让我们打开子图并开始构建。这里不会直接使用对象,而是使用变量。因此,该逻辑将来适用于任何图片。好的,为了与主图发送和接收信息,我们将使用此子图的输入和输出。如果这仍然有点令人困惑,别担心。随着我们的推进,你会清楚地看到它是如何工作的。好的,让我们先从 Y 轴上的连续运动开始,即让图像向下移动。
First, create a new variable called position Y. Then, drag in both a get variable and a set variable node. Since we need to update this value, the idea is simple. Every frame, we will subtract a value from position Y. That small drop frame after frame, it's what moves the image down. And it's the basic idea behind object movement in most games. Before we continue, let's also create a flow input for this subgraph and call it update.
首先,创建一个名为 position Y 的新变量。然后,拖入一个获取变量节点和一个设置变量节点。由于我们需要更新这个值,思路很简单:每一帧,我们从 position Y 中减去一个值。这种逐帧的小幅下降,就是让图像向下移动的原因。这也是大多数游戏中对象移动的基本原理。在继续之前,让我们也为这个子图创建一个流程输入,并将其命名为 update。
Let's grab a subtract node. Then connect position Y into the first input and connect the result back into position Y itself. For the second input, add a test value. Let's say uh two. That means the image drops two pixels every single frame. Next, let's add two outputs. First, a flow output called update. So our logic can continue outside this subgraph. Then add a second output position Y. Now let's connect the flow. Take the update input and connect into the set variable node.
让我们添加一个减法节点。然后将位置 Y 连接到第一个输入,并将结果连接回位置 Y 本身。对于第二个输入,添加一个测试值。比如说,2。这意味着图像每帧下降两个像素。接下来,让我们添加两个输出。首先是一个名为 update 的流输出。这样我们的逻辑可以在这个子图之外继续。然后添加第二个输出 position Y。现在让我们连接流。将 update 输入连接到 set variable 节点。
So this logic only runs when trigger it from outside. Then connect the set variable node into the updates output we just created. Now let's duplicate the get variable node and connect this copy into the position Y output. With that ready, let's head back to the main graph. Out here, grab a frame update node and connect it to our subgraphs input. Then add a set property node. Select our image and choose the y-osition property.
因此,只有从外部触发时,此逻辑才会运行。然后将 set variable 节点连接到我们刚刚创建的 updates 输出。现在让我们复制 get variable 节点,并将此副本连接到 position Y 输出。准备就绪后,让我们回到主图。在这里,获取一个 frame update 节点并将其连接到我们的子图输入。然后添加一个 set property 节点。选择我们的图像并选择 y-osition 属性。
Connect everything. And we're done with just three nodes in the main graph. Our falling logic is already working. Clean and simple. Different devices run our effect at different frame rates. Newer ones might hit 30 frames per second and older ones might only reach 20. If we just subtract a fixed value every frame, the image will fall faster on faster devices and slower on slower one. So the following speed depends on the device and that's a problem.
连接所有内容。我们仅用三个节点就完成了主图的操作。我们的下落逻辑已经生效。简洁明了。不同设备以不同的帧率运行我们的效果。较新的设备可能达到每秒 30 帧,而较旧的设备可能只能达到 20 帧。如果我们每帧减去一个固定值,图像在更快的设备上会下落得更快,在较慢的设备上则更慢。因此,以下速度取决于设备,这是一个问题。
Luckily, this is easy to fix. All we need to do is multiply the value we are subtracting by the frame interval. So let's grab the effect time node and a multiply node and connect it into our current logic. Since we will likely have a different speeds from different image later, let's also add a new input called velocity and connect into the multiply node. This make it easy to control each image speed from the main graph.
幸运的是,这很容易修复。我们需要做的只是将我们要减去的值乘以帧间隔。所以让我们获取 effect time 节点和一个 multiply 节点,并将其连接到我们当前的逻辑中。由于我们稍后可能会有来自不同图像的不同速度,让我们还添加一个名为 velocity 的新输入,并将其连接到 multiply 节点。这使得我们可以轻松地从主图控制每个图像的速度。
Since the frame interval is usually a really small number, we need to increase our speed value to compensate. Let's go from two to something like 600. One thing to notice, our image starts at the center of the screen since position Y starts at zero. Let's fix that. Let's go to the variables panel and set position wise starting value to 900 instead of zero. This way, the image begins offcreen right from the start. Just to be safe, let's also move the image up to 900 in the viewport, too.
由于帧间隔通常是一个非常小的数字,我们需要增加我们的速度值以进行补偿。让我们从 2 改为类似 600 的值。需要注意的一点是,由于位置 Y 从零开始,我们的图像从屏幕中心开始。让我们解决这个问题。让我们转到变量面板,并将 position wise 起始值设置为 900 而不是零。这样,图像从一开始就在屏幕外。为了安全起见,让我们在视口中也将图像向上移动到 900。
Okay, our emotion logic is coming together, but we still need a reset logic. Once the image reach the bottom of the screen, it should spawn again at the top and the cycle repeats. This is simple to do. Before updating position Y, we check if it's less than - 900. So, let's grab an if node and a less than node. Then compare position Y to minus 900. If its condition is true, set it back to the top position. If it's false, keep updating it normally, just like before.
好的,我们的情绪逻辑正在成型,但我们仍然需要一个重置逻辑。一旦图像到达屏幕底部,它应该从顶部重新生成,循环重复。这很简单实现。在更新 Y 位置之前,我们检查它是否小于 -900。所以,让我们获取一个 if 节点和一个小于节点。然后将 Y 位置与 -900 进行比较。如果条件为真,将其重置回顶部位置。如果为假,则像以前一样正常更新它。
That's it. The image resets every time it reads the bottom. Let's use this moment to add one more output, a flow output, and call it reset. This export our resino, and we'll use it later. It also keep things organized. You can also change this resat value. Try - 300 for example. And you see it reset much earlier. You can pick what works bashed. So I will keep - 900. Now it's time to add a horizontal spawning. So the image doesn't always appears in the center.
就是这样。图像每次读取到底部时都会重置。让我们利用这个时刻添加另一个输出,即流输出,并将其命名为 reset(重置)。这将导出我们的 resino(注:原文可能为 typo,指代 reset 或相关变量),稍后我们会用到它。这也使事情保持条理清晰。你也可以更改这个 resat(注:原文可能为 typo,指代 reset)值。例如尝试 -300。你会看到它在更早的时候重置。你可以根据基础情况选择合适的工作方式。所以我保留 -900。现在该添加水平生成了,这样图像就不会总是出现在中心。
Let's work on the X position. You start by creating a variable called position X and add both get variable and set variable nodes. Then grab a random node. Set a minimum and maximum value for X position and connect it to the set variable. You can move the image around in the viewport to get a few for a good minimum and maximum values. I'll go with minus 310 as the minimum and 310 as the maximum. Let's connect this into our rosette output.
让我们处理 X 位置。首先创建一个名为 position X(X 位置)的变量,并添加 get variable(获取变量)和 set variable(设置变量)节点。然后获取一个 random(随机)节点。为 X 位置设置最小值和最大值,并将其连接到 set variable(设置变量)。你可以在视口中移动图像以获取一些良好的最小值和最大值。我将使用 -310 作为最小值,310 作为最大值。让我们将这个连接到我们的 rosette(注:原文可能为 typo,指代 reset)输出。
So every time the image reach the bottom and resets. Let's also grab a start node and connect it into the same logic. So the position is already random when the effect begins. Now let's send this information to the main graph. Click the pencil icon to edit our position Y output. Change its type to 2D vector and rename it to position XY. Grab a combine into vector node and connect it to this output. Then plug in position x and position y.
因此,每次图像到达底部并重置时。让我们还获取一个 start(开始)节点并将其连接到相同的逻辑中。这样当效果开始时,位置已经是随机的了。现在让我们将此信息发送到主图。点击铅笔图标以编辑我们的 position Y(Y 位置)输出。将其类型更改为 2D vector(二维向量),并将其重命名为 position XY(XY 位置)。获取一个 combine into vector(组合为向量)节点并将其连接到此输出。然后插入 position x(X 位置)和 position y(Y 位置)。
This combine both positions into a single vector. Finally, let's update the main graph too. In our set position node, change y position to judge position, which is a 2D vector type. So now every time the image resets it appears in the new spot on the X-axis. Perfect. Now let's move on to our next important step the collision logic. For that we need some kind of reference for the mouth position. Let's create an image under face effects.
这将两个位置合并为一个单一向量。最后,让我们也更新主图。在我们的 set position(设置位置)节点中,将 y position(Y 位置)更改为 judge position(注:原文可能为 typo,指代 joint position 或类似含义,但按字面译为 judge position),这是一个 2D vector(二维向量)类型。所以现在每次图像重置时,它都会出现在 X 轴上的新位置。完美。现在让我们继续到下一个重要步骤:碰撞逻辑。为此,我们需要某种关于嘴巴位置的参考。让我们在 face effects(面部效果)下创建一个图像。
Then the face accessory and add image. This give us a face sticker in the center of the face. So let's rename it to collider and place it on the mouth. To do that, just go to the face accessory object and under ensure placement, select mouth. To check if our falling image is colliding with the mouth, we can use a really useful node called object contains position together with an if node. So, let's grab it. So, on every update, we check if the object contains that position.
然后添加面部配件和图像。这会在面部中心生成一个面部贴纸。因此,让我们将其重命名为 collider(碰撞体)并放置在嘴巴位置。要做到这一点,只需进入面部配件对象,在 ensure placement(确保放置)下选择 mouth(嘴巴)。为了检查我们的下落图像是否与嘴巴发生碰撞,我们可以使用一个非常实用的节点,称为 object contains position(对象包含位置),并结合 if 节点。所以,让我们获取它。这样,在每次更新时,我们检查对象是否包含该位置。
And if it does, we know it's colliding. Let's do a quick test and temporarily change the image color just to preview this. I'll set it to green when it's colliding and red when it's not. If it doesn't feel like it's colliding correctly, try increasing the size of the collider a little. Okay, there is still one small adjustment to make. Since this check runs many times per second while the image stay inside the mouth area, it keeps saying it's colliding over and over for several frames in a row.
如果确实包含,我们就知道发生了碰撞。让我们做一个快速测试,临时更改图像颜色以便预览。当发生碰撞时,我将其设置为绿色;未发生碰撞时,设置为红色。如果觉得碰撞检测不正确,可以尝试稍微增大碰撞体的尺寸。好的,还有一个小调整需要完成。由于此检查每秒运行多次,而图像停留在嘴巴区域内,它会连续多帧反复提示正在碰撞。
That means one single catch could trigger our Sino many many times and that's a problem especially once we get to scoring. So we need to limit how many times the signal fires. Effect maker has a useful node for this called limi
这意味着单次接住动作可能会触发我们的信号 Sino 无数次,这是一个问题,尤其是在涉及计分的时候。因此,我们需要限制信号触发的次数。Effect Maker 有一个有用的节点用于此目的,称为 limi
原文超出正文长度上限,此处截断——上游还有内容,完整版见上方「原文 ↗」。
更进一步:量化金融体系
看懂新闻只是起点——沿量化金融路径,把它变成能交付的工程能力