Skip to content

Commit

Permalink
Add camera smoothing
Browse files Browse the repository at this point in the history
  • Loading branch information
mrezai committed Jul 17, 2024
1 parent a4bf8b2 commit bab017d
Showing 1 changed file with 47 additions and 1 deletion.
48 changes: 47 additions & 1 deletion COGITO/CogitoObjects/cogito_player.gd
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,16 @@ var slide_audio_player : AudioStreamPlayer3D
@onready var test_motion_result: PhysicsTestMotionResult3D = PhysicsTestMotionResult3D.new()

@onready var wieldables = %Wieldables

# Camera Smoothing
@onready var camera_target = $Body/Neck/Head/Eyes
@onready var wieldables_target = $Body/Neck/Head

var is_transforms_update_needed : bool = false
var camera_gt_previous : Transform3D
var camera_gt_current : Transform3D
var wieldables_gt_previous : Transform3D
var wieldables_gt_current : Transform3D
#endregion


Expand Down Expand Up @@ -216,7 +226,19 @@ func _ready():
print("Player has no reference to pause menu.")

call_deferred("slide_audio_init")


camera.set_as_top_level(true)
camera.global_transform = camera_target.global_transform

camera_gt_previous = camera_target.global_transform
camera_gt_current = camera_target.global_transform

wieldables.set_as_top_level(true)
wieldables.global_transform = wieldables_target.global_transform

wieldables_gt_previous = wieldables_target.global_transform
wieldables_gt_current = wieldables_target.global_transform


func slide_audio_init():
#setup sound effect for sliding
Expand Down Expand Up @@ -414,7 +436,31 @@ func _process_on_ladder(_delta):

var jumped_from_slide = false


func update_transforms():
camera_gt_previous = camera_gt_current
camera_gt_current = camera_target.global_transform

wieldables_gt_previous = wieldables_gt_current
wieldables_gt_current = wieldables_target.global_transform


func _process(delta: float) -> void:
if is_transforms_update_needed:
update_transforms()
is_transforms_update_needed = false

var interpolation_fraction : float = clamp(Engine.get_physics_interpolation_fraction(), 0, 1)

var camera_xform : Transform3D = camera_gt_previous.interpolate_with(camera_gt_current, interpolation_fraction)
camera.global_transform = camera_xform

var wieldables_xform : Transform3D = wieldables_gt_previous.interpolate_with(wieldables_gt_current, interpolation_fraction)
wieldables.global_transform = wieldables_xform


func _physics_process(delta):
is_transforms_update_needed = true
#if is_movement_paused:
#return

Expand Down

0 comments on commit bab017d

Please sign in to comment.