Skip to content

Instantly share code, notes, and snippets.

@zvodd
Forked from Einlander/freecam.gd
Created August 28, 2019 16:00
Show Gist options
  • Save zvodd/77ec7453e6c10e066a251bca10bef1cf to your computer and use it in GitHub Desktop.
Save zvodd/77ec7453e6c10e066a251bca10bef1cf to your computer and use it in GitHub Desktop.
free flight camera for godot
extends Camera
# tested in Godot 3.1
export var normal_flyspeed = 5
export var shift_flyspeed = 15
var flyspeed = normal_flyspeed
var tmp_flyspeed
var view_sensitivity = 0.3
var mousedifference = Vector3()
var yaw = 0
var pitch = 0
func _ready():
self.set_process_input(true)
self.set_process(true)
func _enter_tree():
Input.set_mouse_mode(Input.MOUSE_MODE_CAPTURED)
func _exit_tree():
Input.set_mouse_mode(Input.MOUSE_MODE_VISIBLE)
func _input(event):
if event is InputEventMouseMotion:
yaw = fmod(yaw - event.relative.x * view_sensitivity , 360)
pitch = max(min(pitch - event.relative.y * view_sensitivity, 90), -90)
self.set_rotation(Vector3(deg2rad(pitch), deg2rad(yaw), 0))
func _process(delta):
# Fly faster if shift is pressed:
if(Input.is_key_pressed(KEY_SHIFT)):
flyspeed = shift_flyspeed
else:
flyspeed = normal_flyspeed
# WASD = move on horizontal plane
if(Input.is_key_pressed(KEY_W)):
self.set_translation(self.get_translation() - get_global_transform().basis*Vector3(0,0,1) * flyspeed * .01)
if(Input.is_key_pressed(KEY_S)):
self.set_translation(self.get_translation() - get_global_transform().basis*Vector3(0,0,1) * flyspeed * -.01)
if(Input.is_key_pressed(KEY_A)):
self.set_translation(self.get_translation() - get_global_transform().basis*Vector3(1,0,0) * flyspeed * .01)
if(Input.is_key_pressed(KEY_D)):
self.set_translation(self.get_translation() - get_global_transform().basis*Vector3(1,0,0) * flyspeed * -.01)
# E/R move upward, Q/F move downwards
if(Input.is_key_pressed(KEY_E) or Input.is_key_pressed(KEY_R)):
self.set_translation(self.get_translation() - get_global_transform().basis*Vector3(0,1,0) * flyspeed * -.01)
if(Input.is_key_pressed(KEY_Q) or Input.is_key_pressed(KEY_F)):
self.set_translation(self.get_translation() - get_global_transform().basis*Vector3(0,1,0) * flyspeed * .01)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment