首页 > 解决方案 > godot AI pong 动作笨重

问题描述

我是 godot 的新手,正在制作一个乒乓球游戏来练习,我尝试制作一个 AI,它可以工作,但是当球靠近球拍时,运动非常笨拙,这是我的代码:

var direction: = Vector2(0.0,0.0)
var velocity:= Vector2(0.0,0.0)

Called when the node enters the scene tree for the first time.
func _ready() -> void:
pass

Called every frame. 'delta' is the elapsed time since the previous frame.
func physicsprocess(delta: float) -> void:
position.x = 135
var ball = getparent().getnode("ball").position
if ball.y < position.y:
direction.y = -1
velocity = speed * direction
moveandslide(velocity)
elif ball.y > position.y:
direction.y = 1
velocity = speed * direction
moveandslide(velocity)```

标签: artificial-intelligenceponggodot

解决方案


如果您不重复以下代码会发生什么?

velocity = speed * direction
moveandslide(velocity)

另外,我假设球拍的速度比球的速度快,在这种情况下,当球拍靠近球并创建这个循环时,你会得到一个结巴的效果:

桨通过球 桨改变方向 球通过桨 桨改变方向...等等。

我没有对此进行测试,但如果您稍微调整代码,这可以解决问题:

if ball.y < (position.y - speed):elif ball.y > (position.y + speed)

正如我所说,我尚未对此进行测试,因此您可能必须更改标志,但这应该有望阻止抽搐。


推荐阅读