Skip to content

Instantly share code, notes, and snippets.

@PranavSK
Last active October 9, 2020 10:52
Show Gist options
  • Save PranavSK/6625df19d529129a16dc922fa8383b73 to your computer and use it in GitHub Desktop.
Save PranavSK/6625df19d529129a16dc922fa8383b73 to your computer and use it in GitHub Desktop.
extends Spatial
onready var camera_arm: = $Arm
export(float) var move_speed: = 10
export(float) var move_weight: = 3.2
export(float) var rotate_speed: = 1
export(float) var rotate_weight: = 5.1
export(Vector3) var up_dir: = Vector3.UP
var _move_velocity: = Vector3.ZERO
var _rotate_velocity: = 0.0
func _process(delta):
var longitudinal_dir: = Vector3(camera_arm.global_transform.basis.z.x, 0, camera_arm.global_transform.basis.z.z)
var lateral_dir: Vector3 = camera_arm.global_transform.basis.x
var dir = (Input.get_action_strength("camera_backward") - Input.get_action_strength("camera_forward")) * longitudinal_dir\
+ (Input.get_action_strength("camera_right") - Input.get_action_strength("camera_left")) * lateral_dir
var ang_dir = Input.get_action_strength("camera_anticlockwise") - Input.get_action_strength("camera_clockwise")
_update_movement(dir, delta)
_update_rotation(ang_dir, delta)
func _update_movement(dir: Vector3, delta: float) -> void:
var n1 = _move_velocity + (move_speed * dir) * (move_weight * move_weight) * delta
var n2 = 1 + move_weight * delta
_move_velocity = n1 / (n2 * n2)
translate(_move_velocity * delta)
func _update_rotation(dir: float, delta: float) -> void:
var n1 = _rotate_velocity + (rotate_speed * dir) * (rotate_weight * rotate_weight) * delta
var n2 = 1 + rotate_weight * delta
_rotate_velocity = n1 / (n2 * n2)
camera_arm.rotate(up_dir, _rotate_velocity * delta)
[gd_scene load_steps=2 format=2]
[ext_resource path="res://camera/CameraController.gd" type="Script" id=1]
[node name="CameraController" type="Spatial"]
script = ExtResource( 1 )
[node name="Arm" type="Spatial" parent="."]
transform = Transform( 0.707107, -0.612373, 0.353553, 0, 0.5, 0.866025, -0.707107, -0.612372, 0.353553, 0, 0, 0 )
[node name="Camera" type="Camera" parent="Arm"]
transform = Transform( 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 7 )
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment