-
-
Save GammaGames/465acceccef14cbd478591b63a9fb1c3 to your computer and use it in GitHub Desktop.
An easy to use screenshake system in Godot 4. Built with areas and simple to modify.
This file contains hidden or 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
# Based on a video by Pefeper: https://youtu.be/1i5SB8Ct1y0 | |
# Shakable.gd | |
extends Area3D | |
@export var camera : Camera3D | |
@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 : FastNoiseLite | |
@export var noise_speed := 50.0 | |
var trauma := 0.0 | |
var time := 0.0 | |
@onready var initial_rotation := camera.rotation as Vector3 | |
func _process(delta): | |
time += delta | |
trauma = max(trauma - delta * trauma_reduction_rate, 0.0) | |
camera.rotation.x = deg_to_rad(initial_rotation.x + max_x * get_shake_intensity() * get_noise_from_seed(0)) | |
camera.rotation.y = deg_to_rad(initial_rotation.y + max_y * get_shake_intensity() * get_noise_from_seed(1)) | |
camera.rotation.z = deg_to_rad(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) | |
### Shaker.gd | |
### COPY THIS CODE INTO ITS OWN FILE! | |
extends Area3D | |
@export var trauma_amount := 0.1 | |
# Calling this method will check for overlapping shakeable nodes, 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
Im on godot 4.3 stable and debugger gives an error: "Invalid acces to property or key 'rotation' on a base of obcject type 'Nil'. refrencing the line 18.