【问题标题】:godot AI pong movement is clunkygodot AI pong 动作笨重
【发布时间】:2020-02-21 20:51:25
【问题描述】:

我是 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-intelligence pong godot


    【解决方案1】:

    如果不重复以下代码会怎样?

    velocity = speed * direction
    moveandslide(velocity)
    

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

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

    我尚未对此进行测试,但如果您稍微调整一下代码,这可能会解决问题:

    if ball.y &lt; (position.y - speed):elif ball.y &gt; (position.y + speed)

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-09-12
      • 1970-01-01
      • 2021-05-03
      • 1970-01-01
      • 1970-01-01
      • 2018-08-31
      • 2018-11-10
      相关资源
      最近更新 更多