如何在戈多中进行火箭跳跃

发布于 2025-01-15 21:41:06 字数 71 浏览 1 评论 0原文

我在使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

甩你一脸翔 2025-01-22 21:41:09

既然你问的是火箭跳跃,我假设你已经有一个可以工作的射弹武器,并且你已经有一个可以工作的角色控制器。

下一步是进行爆炸。继续制作爆炸场景。当射弹与场景或其他角色碰撞时,您将生成爆炸场景,并 queue_free 射弹。

对于爆炸场景,您可能想要设置一个粒子系统,让它播放预定的时间,然后也queue_free爆炸场景。但这不是与火箭跳跃相关的部分。

您需要的是爆炸场景中的一个Area,它代表爆炸半径。这个想法是,Area 内的所有物体(触发 Area 上的 "body_entered" 的所有物体)都可能受到损坏并被推开。 如果您想让角色隐藏起来,您可以使用 RayCast 进一步细化。但同样,这与火箭跳跃无关,因此我建议在没有 RayCast 的情况下让它工作,如果需要,您可以稍后添加它。


好吧,所以您得到了一个 Area"body_entered" 信号中的 body 表示射弹碰撞时产生的爆炸的爆炸半径。然后呢?

你需要一个从爆炸到那个物体的向量。那就像这样:

var vector := body.global_transform.origin - global_transform.origin

我提醒你,这在“body_entered”信号的函数中。

事实上,我们需要单位向量:

var vector := body.global_transform.origin - global_transform.origin
var direction := vector.normalized()

和距离:

var vector := body.global_transform.origin - global_transform.origin
var direction := vector.normalized()
var distance := vector.length()

平方:

var vector := body.global_transform.origin - global_transform.origin
var direction := vector.normalized()
var distance_squared := vector.length_squared()

现在,我们的想法是,您将对主体应用与距离平方的倒数成正比的速度。您希望速度是可配置的,并根据游戏的需要对其进行调整,因此在脚本顶部添加一个导出变量:

var export speed := 10.0

我们计算速度:

var vector := body.global_transform.origin - global_transform.origin
var direction := vector.normalized()
var distance_squared := vector.length_squared()
var velocity := direction * speed/distance_squared

我正在尝试模仿平方反比定律。但是,这是您的游戏,如果您发现使用 distance 而不是 distance_squared 效果更好,或者您想让速度衰减不同,或者根本不这样做,请转到提前并更改它。


如果主体是RigidBody,我们可以应用脉冲或设置线速度。 无论如何你可能都应该这样做,这样爆炸就可以击倒其他物体。但是你的玩家角色可能是一个KinematicBody,对吧?

现在,我不知道你的玩家角色的代码是什么样的(你还没有将你的代码添加到问题中),所以我不知道你会对其应用速度。

然而,大多数针对玩家角色的教程都会让你设置一个速度矢量,我们的想法是我们将计算出的速度添加到它上面。假设你的类似,那应该是这样的:

body.velocity += velocity

但是,当然,这应该只适用于你的角色。所以你应该对此进行过滤。如果它们有class_name,您可能可以这样做(假设它们是class_nameCharacter):

if body is Character:
    body.velocity += velocity

顺便说一句,这将以相同的速度推动所有东西。它可能应该更多地推动较轻的物体,更少地推动较重的物体。 对于 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 the Area (everything that triggers "body_entered" on the Area) can take damage and can be pushed away. You can further refine using a RayCast if you want allow characters to take cover. But again, that is not relevant for rocket jumping, so I suggest get it working without the RayCast and you can add it later if you want.


Alright, so you got a body in the "body_entered" signal of the Area 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:

var vector := body.global_transform.origin - global_transform.origin

I remind you, this goes in the function for the "body_entered" signal.

In fact, we want the unit vector:

var vector := body.global_transform.origin - global_transform.origin
var direction := vector.normalized()

And the distance:

var vector := body.global_transform.origin - global_transform.origin
var direction := vector.normalized()
var distance := vector.length()

Squared:

var vector := body.global_transform.origin - global_transform.origin
var direction := vector.normalized()
var distance_squared := vector.length_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:

var export speed := 10.0

And we compute the velocity:

var vector := body.global_transform.origin - global_transform.origin
var direction := vector.normalized()
var distance_squared := vector.length_squared()
var velocity := direction * speed/distance_squared

I'm trying to mimic the inverse square law. But, it is your game, if you find that using the distance instead of distance_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 a KinematicBody, 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:

body.velocity += velocity

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 are class_name Character):

if body is Character:
    body.velocity += velocity

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文