Skip to content

Instantly share code, notes, and snippets.

@jegor377
Created December 1, 2020 09:06
Show Gist options
  • Save jegor377/606da0d70c4089b1e2456d25b12af46d to your computer and use it in GitHub Desktop.
Save jegor377/606da0d70c4089b1e2456d25b12af46d to your computer and use it in GitHub Desktop.
Godot's Orthogonal Camera3D Shake Script based on https://godotengine.org/qa/438/camera2d-screen-shake-extension?show=438#q438
extends Camera
var _duration = 0.0
var _period_in_ms = 0.0
var _amplitude = 0.0
var _timer = 0.0
var _last_shook_timer = 0
var _previous_x = 0.0
var _previous_z = 0.0
var _last_offset: Vector3
var _offset: Vector3
onready var _original_pos: Vector3 = translation
func _ready():
set_process(false)
# Shake with decreasing intensity while there's time remaining.
func _process(delta):
# Only shake when there's shake time remaining.
if _timer == 0:
_offset = Vector3.ZERO
set_process(false)
return
# Only shake on certain frames.
_last_shook_timer = _last_shook_timer + delta
# Be mathematically correct in the face of lag; usually only happens once.
while _last_shook_timer >= _period_in_ms:
_last_shook_timer = _last_shook_timer - _period_in_ms
# Lerp between [amplitude] and 0.0 intensity based on remaining shake time.
var intensity = _amplitude * (1 - ((_duration - _timer) / _duration))
# Noise calculation logic from http://jonny.morrill.me/blog/view/14
var new_x = rand_range(-1.0, 1.0)
var x_component = intensity * (_previous_x + (delta * (new_x - _previous_x)))
var new_z = rand_range(-1.0, 1.0)
var z_component = intensity * (_previous_z + (delta * (new_z - _previous_z)))
_previous_x = new_x
_previous_z = new_z
# Track how much we've moved the offset, as opposed to other effects.
var new_offset = Vector3(x_component, 0.0, z_component)
_offset = _offset - _last_offset + new_offset
_last_offset = new_offset
# Reset the offset when we're done shaking.
_timer = _timer - delta
if _timer <= 0:
_timer = 0
_offset = Vector3.ZERO
translation = _original_pos + _offset
# Kick off a new screenshake effect.
func shake(duration, frequency, amplitude):
# Don't interrupt current shake duration
if(_timer != 0):
return
# Initialize variables.
_duration = duration
_timer = duration
_period_in_ms = 1.0 / frequency
_amplitude = amplitude
_previous_x = rand_range(-1.0, 1.0)
_previous_z = rand_range(-1.0, 1.0)
# Reset previous offset, if any.
_offset = _offset - _last_offset
_last_offset = Vector3.ZERO
set_process(true)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment