如何在 Godot 中让 2D 精灵跟随鼠标

Tim*_*rus 2 godot

请提供 Godot 中跟随鼠标的 Sprite 的最小代码示例。有复杂的和大的示例项目,但我没有发现任何小而清晰的东西。

Yan*_*anb 5

Sprite节点放入场景中,并将以下脚本附加到它。

const SPEED = 500

func _process(delta):
    var vec = get_viewport().get_mouse_position() - self.position # getting the vector from self to the mouse
    vec = vec.normalized() * delta * SPEED # normalize it and multiply by time and speed
    position += vec # move by that vector
Run Code Online (Sandbox Code Playgroud)