Skip to content

Instantly share code, notes, and snippets.

@Einlander
Last active May 4, 2020 12:35
Show Gist options
  • Save Einlander/4a408e91679d32a9c063 to your computer and use it in GitHub Desktop.
Save Einlander/4a408e91679d32a9c063 to your computer and use it in GitHub Desktop.
free flight camera for godot
extends Camera
# member variables here, example:
# var a=2
# var b="textvar"
export var flyspeed= 0.1
var view_sensitivity = 0.3
var defaulCam = Matrix3()
var mousedifference = Vector3()
var yaw = 0
var pitch = 0
func _ready():
# Initialization here
self.set_process_input(true)
self.set_process(true)
#set mouse position
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.type == InputEvent.KEY):
if(event.scancode == KEY_ESCAPE):
self.get_tree().quit()
if event.type == InputEvent.MOUSE_MOTION:
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), 0 , 0))
self.set_rotation(Vector3(0, deg2rad(yaw), 0))
func _process(delta):
#mouse movement
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)
@RayKoopa
Copy link

RayKoopa commented Sep 8, 2016

I'd this supposed to work? The second set_rotation call removes the effect of the pitch rotation before, thus you can only rotate around the Y axis.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment