Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
Godot Orbital Camera Controller
extends Camera
export var follow_target_path:NodePath = ""
var follow_target:Node
export var follow_distance:float = 5.0
export var follow_height:float = 1.0
export var mouse_sensitivity_x:float = 0.005
export var mouse_sensitivity_y:float = 0.005
var last_mouse_delta:Vector2 = Vector2()
var mouse_accumulator:Vector2 = Vector2() # The reason we have this instead of using the mouse.position is 'cause when we capture the camera, this will always be 0.
func _ready():
follow_target = get_node(follow_target_path)
func _process(delta):
# Looking. Rotate _this_. Tilt _camera_.
mouse_accumulator += last_mouse_delta # TODO: Cap the Y component.
last_mouse_delta = Vector2() # Reset after we process the mouse so we don't accumulate between events.
# Calculate our new position.
var look_target = follow_target.global_transform.origin + Vector3(0, follow_height, 0)
var target_position = look_target + \
Vector3(cos(mouse_accumulator.x*mouse_sensitivity_x), sin(mouse_accumulator.y*mouse_sensitivity_y), sin(mouse_accumulator.x*mouse_sensitivity_x))*follow_distance
self.look_at_from_position(target_position, look_target, Vector3(0, 1, 0))
# For mouse handling:
func _input(event):
if event is InputEventMouseMotion:
#event.position Will always be center when in capture mode. Same with event.speed.
last_mouse_delta = event.relative
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment