如何在 Godot 中制作一个 2D 精灵跟随鼠标
How to make a 2D sprite follow mouse in Godot
请提供Godot 中Sprite 跟随鼠标的最小代码示例。样例项目复杂又大,但我没发现小而清晰的东西。
将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
请提供Godot 中Sprite 跟随鼠标的最小代码示例。样例项目复杂又大,但我没发现小而清晰的东西。
将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