artificial-intelligence - 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)```
解决方案
如果您不重复以下代码会发生什么?
velocity = speed * direction
moveandslide(velocity)
另外,我假设球拍的速度比球的速度快,在这种情况下,当球拍靠近球并创建这个循环时,你会得到一个结巴的效果:
桨通过球 桨改变方向 球通过桨 桨改变方向...等等。
我没有对此进行测试,但如果您稍微调整代码,这可以解决问题:
if ball.y < (position.y - speed):
和elif ball.y > (position.y + speed)
正如我所说,我尚未对此进行测试,因此您可能必须更改标志,但这应该有望阻止抽搐。
推荐阅读
- excel - VBA数据透视表代码在单步执行时有效,但在分配给宏按钮时无效
- python - Python3.6 函数未调用
- unicode - 无法使用 cqlsh 将 UTF-8 数据复制到 ScyllaDB
- javascript - React 中的条件样式
- spring-integration - 确认春季消息
- javascript - 从特定行访问表格单元格以检索其嵌入的输入值
- python - 如果条件使用 Python
- javascript - Vue 谷歌地图信息窗口自定义按钮不起作用
- java - 如何在句子中挑选主语、谓语、宾语和形容词
- python - 在不使用 scipy python 的情况下计算距离矩阵