Created
December 24, 2021 19:41
-
-
Save sjvnnings/81bc572e4614832a442ee9a81e8ae24f to your computer and use it in GitHub Desktop.
An easy to use screenshake system in Godot. Built with areas and simple to modify.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#For usage instructions, see the YouTube video below: | |
extends Area | |
export var trauma_reduction_rate := 1.0 | |
export var max_x := 10.0 | |
export var max_y := 10.0 | |
export var max_z := 5.0 | |
export var noise : OpenSimplexNoise | |
export var noise_speed := 50.0 | |
var trauma := 0.0 | |
var time := 0.0 | |
onready var camera := $Camera as Camera | |
onready var initial_rotation := camera.rotation_degrees as Vector3 | |
func _process(delta): | |
time += delta | |
trauma = max(trauma - delta * trauma_reduction_rate, 0.0) | |
camera.rotation_degrees.x = initial_rotation.x + max_x * get_shake_intensity() * get_noise_from_seed(0) | |
camera.rotation_degrees.y = initial_rotation.y + max_y * get_shake_intensity() * get_noise_from_seed(1) | |
camera.rotation_degrees.z = initial_rotation.z + max_z * get_shake_intensity() * get_noise_from_seed(2) | |
func add_trauma(trauma_amount : float): | |
trauma = clamp(trauma + trauma_amount, 0.0, 1.0) | |
func get_shake_intensity() -> float: | |
return trauma * trauma | |
func get_noise_from_seed(_seed : int) -> float: | |
noise.seed = _seed | |
return noise.get_noise_1d(time * noise_speed) | |
### trauma_causer.gd | |
### COPY THIS CODE INTO ITS OWN FILE! | |
extends Area | |
export var trauma_amount := 0.1 | |
#Calling this method will check for all shakeable cameras the "trauma causer" overlaps with, then increase the screenshake intensity. | |
func cause_trauma(): | |
var trauma_areas := get_overlapping_areas() | |
for area in trauma_areas: | |
if area.has_method("add_trauma"): | |
area.add_trauma(trauma_amount) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Forked for 4: https://gist.github.com/GammaGames/465acceccef14cbd478591b63a9fb1c3