-
Notifications
You must be signed in to change notification settings - Fork 0
/
ShakeCamera2D.gd
43 lines (35 loc) · 1.31 KB
/
ShakeCamera2D.gd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# https://kidscancode.org/godot_recipes/2d/screen_shake/
extends Camera2D
class_name ShakeCamera2D
export var decay = 0.8 # How quickly the shaking stops [0, 1].
export var max_offset = Vector2(100, 50) # Maximum hor/ver shake in pixels.
export var max_roll = 0.1 # Maximum rotation in radians (use sparingly).
export (NodePath) var target # Assign the node this camera will follow.
var trauma = 0.0 # Current shake strength.
var trauma_power = 2 # Trauma exponent. Use [2, 3].
onready var noise = OpenSimplexNoise.new()
var noise_y = 0
func _ready():
randomize()
noise.seed = randi()
noise.period = 4
noise.octaves = 2
func _process(delta):
if target:
global_position = get_node(target).global_position
if trauma:
trauma = max(trauma - decay * delta, 0)
shake()
func shake():
var amount = pow(trauma, trauma_power)
noise_y += 1
# Using noise
rotation = max_roll * amount * noise.get_noise_2d(noise.seed, noise_y)
offset.x = max_offset.x * amount * noise.get_noise_2d(noise.seed*2, noise_y)
offset.y = max_offset.y * amount * noise.get_noise_2d(noise.seed*3, noise_y)
# Pure randomness
# rotation = max_roll * amount * rand_range(-1, 1)
# offset.x = max_offset.x * amount * rand_range(-1, 1)
# offset.y = max_offset.y * amount * rand_range(-1, 1)
func add_trauma(amount):
trauma = min(trauma + amount, 1.0)