如何在戈多中进行火箭跳跃
我在使用 GDscript 在 Godot 中制作火箭跳跃时遇到了一些问题,我无法在此处添加任何代码,因为我尝试做的一切总是失败
I have been having some problems in making rocket jumping in Godot using GDscript I cannot add any code here as everything i try to do always fails
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
既然你问的是火箭跳跃,我假设你已经有一个可以工作的射弹武器,并且你已经有一个可以工作的角色控制器。
下一步是进行爆炸。继续制作爆炸场景。当射弹与场景或其他角色碰撞时,您将生成爆炸场景,并
queue_free
射弹。对于爆炸场景,您可能想要设置一个粒子系统,让它播放预定的时间,然后也
queue_free
爆炸场景。但这不是与火箭跳跃相关的部分。您需要的是爆炸场景中的一个
Area
,它代表爆炸半径。这个想法是,Area
内的所有物体(触发Area
上的"body_entered"
的所有物体)都可能受到损坏并被推开。 如果您想让角色隐藏起来,您可以使用RayCast
进一步细化。但同样,这与火箭跳跃无关,因此我建议在没有RayCast
的情况下让它工作,如果需要,您可以稍后添加它。好吧,所以您得到了一个
表示射弹碰撞时产生的爆炸的爆炸半径。然后呢?Area
的"body_entered"
信号中的 body你需要一个从爆炸到那个物体的向量。那就像这样:
我提醒你,这在
“body_entered”
信号的函数中。事实上,我们需要单位向量:
和距离:
平方:
现在,我们的想法是,您将对主体应用与距离平方的倒数成正比的速度。您希望速度是可配置的,并根据游戏的需要对其进行调整,因此在脚本顶部添加一个导出变量:
我们计算速度:
我正在尝试模仿平方反比定律。但是,这是您的游戏,如果您发现使用
distance
而不是distance_squared
效果更好,或者您想让速度衰减不同,或者根本不这样做,请转到提前并更改它。如果主体是
RigidBody
,我们可以应用脉冲或设置线速度。 无论如何你可能都应该这样做,这样爆炸就可以击倒其他物体。但是你的玩家角色可能是一个KinematicBody
,对吧?现在,我不知道你的玩家角色的代码是什么样的(你还没有将你的代码添加到问题中),所以我不知道你会对其应用速度。
然而,大多数针对玩家角色的教程都会让你设置一个速度矢量,我们的想法是我们将计算出的速度添加到它上面。假设你的类似,那应该是这样的:
但是,当然,这应该只适用于你的角色。所以你应该对此进行过滤。如果它们有
class_name
,您可能可以这样做(假设它们是class_nameCharacter
):顺便说一句,这将以相同的速度推动所有东西。它可能应该更多地推动较轻的物体,更少地推动较重的物体。 对于 RigidBody,您将施加力而不是脉冲。因此您可以声明角色中的质量并考虑计算速度。 但是,再次强调,首先让简单版本正常工作。
Since you are asking about rocket jumping, I'll assume you already have a projectile weapon working, and you already have a character controller working.
The next step is to make an explosion. Go ahead and make an explosion scene. When the projectile collides with the scenario or another character, you will spawn the explosion scene, and
queue_free
the projectile.For the explosion scene, you probably want to set up a particle system, let it play for a predetermined time, and then
queue_free
the explosion scene too. But that is not the part that is relevant for rocket jumping.What you need is an
Area
in the explosion scene, that represent the blast radius. The idea is that everything inside theArea
(everything that triggers"body_entered"
on theArea
) can take damage and can be pushed away. You can further refine using aRayCast
if you want allow characters to take cover. But again, that is not relevant for rocket jumping, so I suggest get it working without theRayCast
and you can add it later if you want.Alright, so you got a
body
in the"body_entered"
signal of theArea
that represents the blast radius of the explosion spawned by the projectile when it collided. Then what?You want a vector that goes from the explosion to that body. That would be like this:
I remind you, this goes in the function for the
"body_entered"
signal.In fact, we want the unit vector:
And the distance:
Squared:
Now, the idea is that you will apply to the body a velocity proportional to the inverse of the distance squared. You want the velocity to be configurable, and tweak it for the needs of your game, so add a export variable on the top of the script:
And we compute the velocity:
I'm trying to mimic the inverse square law. But, it is your game, if you find that using the
distance
instead ofdistance_squared
works better, or you want to make the velocity decay differently, or not at all, go ahead and change it.If the body is a
RigidBody
we can apply an impulse or set the linear velocity. And you probably should do that anyway, so the explosion can knock down other objects. But your player character probably is aKinematicBody
, right?Now, I don't know how the code for your player character looks like (you haven't added your code to the question), so I don't know you would apply a velocity to it.
However, most tutorials for player character will have you set a velocity vector, and the idea is that we will add the velocity we computed to it. Assuming yours is similar, that should be something like this:
But, of course, that should only be for your characters. So you should filter for that. If they have a
class_name
you probably can do this (assuming here that they areclass_name Character
):By the way, this will push everything with the same velocity. It probably should push lighter object more and heavier objects less. For the RigidBody, you would apply a force instead of an impulse. So you could declare a mass in your characters and take into account for computing the velocity. But, again, get the simple version working first.