mirror of
https://github.com/godotengine/godot-docs.git
synced 2026-09-03 18:14:53 +03:00
Merge pull request #11591 from Calinou/mouse-motion-screen-relative
Use and recommend `InputEventMouseMotion.screen_relative` where applicable
This commit is contained in:
@@ -107,11 +107,12 @@ Add an ``_unhandled_input`` function to check for camera movement and then rotat
|
||||
|
||||
|
||||
func _unhandled_input(event: InputEvent) -> void:
|
||||
# Mouselook implemented using `screen_relative` for resolution-independent sensitivity.
|
||||
if event is InputEventMouseMotion:
|
||||
_camera_pivot.rotation.x -= event.relative.y * mouse_sensitivity
|
||||
_camera_pivot.rotation.x -= event.screen_relative.y * mouse_sensitivity
|
||||
# Prevent the camera from rotating too far up or down.
|
||||
_camera_pivot.rotation.x = clampf(_camera_pivot.rotation.x, -tilt_limit, tilt_limit)
|
||||
_camera_pivot.rotation.y += -event.relative.x * mouse_sensitivity
|
||||
_camera_pivot.rotation.y += -event.screen_relative.x * mouse_sensitivity
|
||||
|
||||
By rotating the pivot point, the spring arm will also be rotated and it will change where the camera is positioned.
|
||||
Run the game and notice that mouse movement now rotates the camera around the character. If the camera moves into a wall, it collides with it.
|
||||
|
||||
@@ -350,8 +350,8 @@ Example of looking around, FPS style:
|
||||
func _input(event):
|
||||
if event is InputEventMouseMotion and event.button_mask & 1:
|
||||
# modify accumulated mouse rotation
|
||||
rot_x -= event.relative.x * LOOKAROUND_SPEED
|
||||
rot_y -= event.relative.y * LOOKAROUND_SPEED
|
||||
rot_x -= event.screen_relative.x * LOOKAROUND_SPEED
|
||||
rot_y -= event.screen_relative.y * LOOKAROUND_SPEED
|
||||
transform.basis = Basis() # reset rotation
|
||||
rotate_object_local(Vector3(0, 1, 0), rot_x) # first rotate in Y
|
||||
rotate_object_local(Vector3(1, 0, 0), rot_y) # then rotate in X
|
||||
@@ -367,8 +367,8 @@ Example of looking around, FPS style:
|
||||
if (@event is InputEventMouseMotion mouseMotion)
|
||||
{
|
||||
// modify accumulated mouse rotation
|
||||
_rotationX -= mouseMotion.Relative.X * LookAroundSpeed;
|
||||
_rotationY -= mouseMotion.Relative.Y * LookAroundSpeed;
|
||||
_rotationX -= mouseMotion.ScreenRelative.X * LookAroundSpeed;
|
||||
_rotationY -= mouseMotion.ScreenRelative.Y * LookAroundSpeed;
|
||||
|
||||
// reset rotation
|
||||
Transform3D transform = Transform;
|
||||
|
||||
@@ -67,4 +67,14 @@ Alternatively, it's possible to ask the viewport for the mouse position:
|
||||
|
||||
GetViewport().GetMousePosition();
|
||||
|
||||
.. note:: When the mouse mode is set to ``Input.MOUSE_MODE_CAPTURED``, the ``event.position`` value from ``InputEventMouseMotion`` is the center of the screen. Use ``event.relative`` instead of ``event.position`` and ``event.velocity`` to process mouse movement and position changes.
|
||||
.. note::
|
||||
|
||||
When the mouse mode is set to ``Input.MOUSE_MODE_CAPTURED``, the ``event.position``
|
||||
value from ``InputEventMouseMotion`` is the center of the screen.
|
||||
Use ``event.relative`` instead of ``event.position`` and ``event.velocity``
|
||||
to process mouse movement and position changes.
|
||||
|
||||
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
|
||||
:ref:`multiple resolutions <doc_multiple_resolutions>`.
|
||||
|
||||
Reference in New Issue
Block a user