Skip to content

Instantly share code, notes, and snippets.

@Garmelon
Last active October 24, 2022 14:12
Show Gist options
  • Save Garmelon/117037219ee64b8eed92bff89b64425d to your computer and use it in GitHub Desktop.
Save Garmelon/117037219ee64b8eed92bff89b64425d to your computer and use it in GitHub Desktop.
extends Camera
"""
Space/Enter - capture cursor
Escape - free cursor
WASDQE - move
Shift - fast movement
Ctrl - slow movement
"""
export (float) var slow_movement_speed: float = 0.2
export (float) var movement_speed: float = 2
export (float) var fast_movement_speed: float = 9
export (Vector2) var mouse_sensitivity: Vector2 = Vector2(0.008, 0.008)
var euler: Vector3
func _ready() -> void:
euler = transform.basis.get_euler()
euler.z = 0
func _input(event: InputEvent) -> void:
if event is InputEventKey:
# Use different actions if these are already used by your game
if event.is_action_pressed("ui_accept"):
Input.set_mouse_mode(Input.MOUSE_MODE_CAPTURED)
elif event.is_action_pressed("ui_cancel"):
Input.set_mouse_mode(Input.MOUSE_MODE_VISIBLE)
if Input.get_mouse_mode() == Input.MOUSE_MODE_CAPTURED and event is InputEventMouseMotion:
euler.y += event.relative.x * -mouse_sensitivity.x
euler.x += event.relative.y * -mouse_sensitivity.y
func _process(delta: float) -> void:
transform.basis = Basis(euler)
var movement: Vector3 = Vector3.ZERO
# Replace these with actions if you want
if Input.is_key_pressed(KEY_W): movement += Vector3.FORWARD
if Input.is_key_pressed(KEY_S): movement += Vector3.BACK
if Input.is_key_pressed(KEY_A): movement += Vector3.LEFT
if Input.is_key_pressed(KEY_D): movement += Vector3.RIGHT
if Input.is_key_pressed(KEY_Q): movement += Vector3.DOWN
if Input.is_key_pressed(KEY_E): movement += Vector3.UP
# And these
movement = movement.normalized() * delta
if Input.is_key_pressed(KEY_SHIFT):
movement *= fast_movement_speed
elif Input.is_key_pressed(KEY_CONTROL):
movement *= slow_movement_speed
else:
movement *= movement_speed
transform.origin += transform.basis * movement
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment