Free Godot 4 flight controller without gimbal lock

Here’s a very simple flight controller for Godot 4 that allows you to make airplane/spaceplane type games without a gimbal lock.

The code is CC0 (public domain) - use at own risk, etc. Copyright 2023 Miguel B, no rights reserved.

https://www.longplay.games/FlightController.zip

Here’s the code of the actual controller itself (the important part)

extends CharacterBody3D

# By Miguel B, Copyright 2023 No Rights reserved, CC0

@export var maxSpeed :float = 10.0
@export var turnRate :float = 0.1
var Speed = 0.0
# Get the project gravity when ready
@onready var Gravity = ProjectSettings.get_setting("physics/3d/default_gravity")

func _physics_process(delta):
	_rotate()
	if Input.is_action_just_pressed("Accelerate") and (Speed <= maxSpeed):
		Speed += 1.0
	if Input.is_action_just_pressed("Decelerate"):
		if (Speed >= 1.0):
			Speed -= 1.0
		else:
			Speed = 0.0
	if (Speed > 0.0):
		global_translate(transform.basis.z)
	move_and_slide()
	pass

func _rotate():
	var yaw = Input.get_axis("YawRight", "YawLeft") # Y Axis
	var roll = Input.get_axis("RollLeft", "RollRight") # Z Axis
	var pitch = Input.get_axis("PitchUp", "PitchDown") # X Axis
	var direction = Vector3(pitch, yaw, roll) * turnRate
	if direction: 
		# This only runs if there's any form of input, clearing out deadzone noise
		# print("Apply direction", direction)
		var a = Quaternion(transform.basis.from_euler(rotation))
		var b = Quaternion(transform.basis.from_euler(direction))
		transform.basis = Basis(a * b)
1 Like

that is awesome!!! :100:

1 Like

Thanks! Hoping it will be useful to someone.

1 Like