Godot放慢了区域2D对象

发布于 2025-01-25 17:59:40 字数 480 浏览 2 评论 0原文

我正在为我的2D自上而下的太空游戏制作一个原型,并且我将aquare2d用于 *船对象,因为它的大多数机械师仅依靠碰撞。有什么办法可以放慢脚步,然后在发射船后停止,因为现在它只是连续飞行。 这是我的基本代码:


var jump_speed = 500
var velocity = Vector2()

func get_input(delta):
    if Input.is_action_pressed("click"):
        look_at(get_global_mouse_position())
    if Input.is_action_just_released("click"):
        velocity = transform.x * jump_speed
        
func _physics_process(delta):
    get_input(delta)
    position -= velocity * delta```

I'm making a prototype for my 2d top-down space game and I'm using area2d for my *ship object since most of its mechanics rely only in collisions. Is there a way i can slow down then stop my ship after it is launched, because as for now it just fly continuously.
Here is my basic code:


var jump_speed = 500
var velocity = Vector2()

func get_input(delta):
    if Input.is_action_pressed("click"):
        look_at(get_global_mouse_position())
    if Input.is_action_just_released("click"):
        velocity = transform.x * jump_speed
        
func _physics_process(delta):
    get_input(delta)
    position -= velocity * delta```

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

岛徒 2025-02-01 17:59:40

总的来说,这个想法是随着时间的推移降低velocity向量的长度。我们可以为此定义减速(这不是唯一的方法):

var deceleration := 10.0

然后,在_PHYSICS_PROCESS中,我们可以通过减少velocity的数量来计算:

var speed_reduction := deceleration * delta

将其与velocity的量表进行比较,如果是更多,只需将velocity设置为vector> vector2.zer.zer.zer.zero否则我们要缩放速度适当:

var current_speed := velocity.length()
if current_speed > speed_reduction:
    velocity = Vector2.ZERO
else:
    velocity = velocity * (current_speed - speed_reduction) / current_speed

顺便说一句,从godot 3.5开始,limit_length vector2vector3使这个更简单地实现。

In general the idea is to reduce the length of the velocity vector over time. We could define a deceleration for that (this is not the only way to go about it):

var deceleration := 10.0

And then in _physics_process we can compute by how much the velocity would be reduced:

var speed_reduction := deceleration * delta

Compare it with the magnitud of velocity, if it is more, just set the velocity to Vector2.ZERO otherwise we want to scale velocity appropriately:

var current_speed := velocity.length()
if current_speed > speed_reduction:
    velocity = Vector2.ZERO
else:
    velocity = velocity * (current_speed - speed_reduction) / current_speed

By the way, from Godot 3.5 onward there is a limit_length method in Vector2 and Vector3 that can make this simpler to implement.

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