Last active
July 12, 2019 05:27
-
-
Save Calinou/820ca6211eef32f531d26e89c4262de9 to your computer and use it in GitHub Desktop.
Freelook camera script from my Sponza demo
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Copyright © 2017 Hugo Locurcio and contributors - MIT license | |
# See LICENSE.md included in the source distribution for more information. | |
extends Camera | |
const MOVE_SPEED = 0.5 | |
const MOUSE_SENSITIVITY = 0.002 | |
onready var speed = 1 | |
onready var velocity = Vector3() | |
onready var initial_rotation = PI/2 | |
func _enter_tree(): | |
# Capture the mouse (can be toggled by pressing F10) | |
Input.set_mouse_mode(Input.MOUSE_MODE_CAPTURED) | |
func _input(event): | |
# Horizontal mouse look | |
if event is InputEventMouseMotion and Input.get_mouse_mode() == Input.MOUSE_MODE_CAPTURED: | |
rotation.y -= event.relative.x*MOUSE_SENSITIVITY | |
# Vertical mouse look, clamped to -90..90 degrees | |
if event is InputEventMouseMotion and Input.get_mouse_mode() == Input.MOUSE_MODE_CAPTURED: | |
rotation.x = clamp(rotation.x - event.relative.y*MOUSE_SENSITIVITY, deg2rad(-90), deg2rad(90)) | |
# Toggle HUD | |
if event.is_action_pressed("toggle_hud"): | |
$"../FPSCounter".visible = !$"../FPSCounter".visible | |
# Toggle mouse capture | |
if event.is_action_pressed("toggle_mouse_capture"): | |
if Input.get_mouse_mode() == Input.MOUSE_MODE_CAPTURED: | |
Input.set_mouse_mode(Input.MOUSE_MODE_VISIBLE) | |
else: | |
Input.set_mouse_mode(Input.MOUSE_MODE_CAPTURED) | |
func _physics_process(delta): | |
# Speed modifier | |
if Input.is_action_pressed("move_speed"): | |
speed = 2 | |
else: | |
speed = 1 | |
# Movement | |
if Input.is_action_pressed("move_forward"): | |
velocity.x -= MOVE_SPEED*speed*delta | |
if Input.is_action_pressed("move_backward"): | |
velocity.x += MOVE_SPEED*speed*delta | |
if Input.is_action_pressed("move_left"): | |
velocity.z += MOVE_SPEED*speed*delta | |
if Input.is_action_pressed("move_right"): | |
velocity.z -= MOVE_SPEED*speed*delta | |
if Input.is_action_pressed("move_up"): | |
velocity.y += MOVE_SPEED*speed*delta | |
if Input.is_action_pressed("move_down"): | |
velocity.y -= MOVE_SPEED*speed*delta | |
# Friction | |
velocity *= 0.875 | |
# Apply velocity | |
translation += velocity \ | |
.rotated(Vector3(0, 1, 0), rotation.y - initial_rotation) \ | |
.rotated(Vector3(1, 0, 0), cos(rotation.y)*rotation.x) \ | |
.rotated(Vector3(0, 0, 1), -sin(rotation.y)*rotation.x) | |
func _exit_tree(): | |
# Restore the mouse cursor upon quitting | |
Input.set_mouse_mode(Input.MOUSE_MODE_VISIBLE) |
# Copyright © 2017 Hugo Locurcio and contributors - MIT license
# Copyright © 2019 Manuel 'TheDuriel' Fischer - MIT license
extends Camera
class_name DebugCamera
const MOVE_SPEED: float = 1.0
const MOUSE_SENSITIVITY: float = 0.002
onready var speed: int = 1
onready var velocity: Vector3 = Vector3()
onready var initial_rotation: float = TAU / 4
func _ready() -> void:
# Capture the mouse (can be toggled by pressing 1)
Input.set_mouse_mode(Input.MOUSE_MODE_CAPTURED)
func _input(event) -> void:
# Horizontal mouse look
if event is InputEventMouseMotion and Input.get_mouse_mode() == Input.MOUSE_MODE_CAPTURED:
rotation.y -= event.relative.x*MOUSE_SENSITIVITY
# Vertical mouse look, clamped to -90..90 degrees
if event is InputEventMouseMotion and Input.get_mouse_mode() == Input.MOUSE_MODE_CAPTURED:
rotation.x = clamp(rotation.x - event.relative.y*MOUSE_SENSITIVITY, deg2rad(-90), deg2rad(90))
# Toggle mouse capture
if event is InputEventKey and event.scancode == KEY_1 and not event.is_echo():
if Input.get_mouse_mode() == Input.MOUSE_MODE_CAPTURED:
Input.set_mouse_mode(Input.MOUSE_MODE_VISIBLE)
else:
Input.set_mouse_mode(Input.MOUSE_MODE_CAPTURED)
func _physics_process(delta: float) -> void:
# Speed modifier
if Input.is_key_pressed(KEY_SHIFT):
speed = 2
else:
speed = 1
# Movement
if Input.is_key_pressed(KEY_W):
velocity.x -= MOVE_SPEED*speed*delta
if Input.is_key_pressed(KEY_S):
velocity.x += MOVE_SPEED*speed*delta
if Input.is_key_pressed(KEY_A):
velocity.z += MOVE_SPEED*speed*delta
if Input.is_key_pressed(KEY_D):
velocity.z -= MOVE_SPEED*speed*delta
if Input.is_key_pressed(KEY_E):
velocity.y += MOVE_SPEED*speed*delta
if Input.is_key_pressed(KEY_Q):
velocity.y -= MOVE_SPEED*speed*delta
# Friction
velocity *= 0.875
# Apply velocity
translation += velocity \
.rotated(Vector3(0, 1, 0), rotation.y - initial_rotation) \
.rotated(Vector3(1, 0, 0), cos(rotation.y)*rotation.x) \
.rotated(Vector3(0, 0, 1), -sin(rotation.y)*rotation.x)
func _exit_tree() -> void:
# Restore the mouse cursor upon quitting
Input.set_mouse_mode(Input.MOUSE_MODE_VISIBLE)
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Translated to C# API