Attention: Here be dragons
This is the latest
(unstable) version of this documentation, which may document features
not available in or compatible with released stable versions of Godot.
Checking the stable version of the documentation...
マウスと入力座標
概要
この小さなチュートリアルの理由は、入力座標、マウスの位置や画面の解像度などに関する多くのよくある間違いを解決するためです。
ハードウェア表示の座標
エディタ、MMO、ツールなど、PC上で実行する複雑なUIを記述する場合、ハードウェア座標の使用は理にかなっています。しかし、その以外ではあまり意味がありません。
ビューポート表示の座標
Godotはビューポートを使用してコンテンツを表示し、ビューポートはいくつかのオプションでスケーリングできます(複数の解像度 チュートリアルを参照)。次に、ノード内の関数を使用して、マウス座標とビューポートサイズを取得します。次に例を示します:
func _input(event):
# Mouse in viewport coordinates.
if event is InputEventMouseButton:
print("Mouse Click/Unclick at: ", event.position)
elif event is InputEventMouseMotion:
print("Mouse Motion at: ", event.position)
# Print the size of the viewport.
print("Viewport Resolution is: ", get_viewport().get_visible_rect().size)
public override void _Input(InputEvent @event)
{
// Mouse in viewport coordinates.
if (@event is InputEventMouseButton eventMouseButton)
{
GD.Print("Mouse Click/Unclick at: ", eventMouseButton.Position);
}
else if (@event is InputEventMouseMotion eventMouseMotion)
{
GD.Print("Mouse Motion at: ", eventMouseMotion.Position);
}
// Print the size of the viewport.
GD.Print("Viewport Resolution is: ", GetViewport().GetVisibleRect().Size);
}
または、ビューポートにマウスの位置を問い合わせるようにします:
get_viewport().get_mouse_position()
GetViewport().GetMousePosition();
注釈
マウスモードが Input.MOUSE_MODE_CAPTURED に設定されている場合、InputEventMouseMotion の event.position 値は画面の中心になります。マウスの移動と位置の変更を処理するには event.position と event.velocity の代わりに event.relative を使用します。
When implementing features such as mouselook, it's recommended to use
event.screen_relative and event.screen_velocity instead of event.relative
and event.velocity so that mouse movement behaves the same across
multiple resolutions.