Take damage/Screen shake

This commit is contained in:
2025-11-30 22:35:26 -08:00
parent 8f8cc217dc
commit 4ee4e02a51
5 changed files with 131 additions and 12 deletions

View File

@@ -0,0 +1,45 @@
using Godot;
public partial class ShakeCamera : Camera3D
{
[Export] private double _shakeIntensity = 1.0;
[Export] private double _maxX = 10;
[Export] private double _maxY = 10;
[Export] private double _maxZ = 5;
[Export] private FastNoiseLite _noise;
[Export] private double _noiseSpeed = 50.0;
private double _shake = 0.0;
private double _time = 0.0;
private Vector3 _initialRotation;
public override void _Ready()
{
_initialRotation = RotationDegrees;
}
public override void _Process(double delta)
{
_time += delta;
_shake = Mathf.Max(_shake - delta * _shakeIntensity, 0.0);
RotationDegrees = new Vector3(
(float)(_initialRotation.X + _maxX * Mathf.Pow(_shake, 2) * GetNoiseFromSeed(0)),
(float)(_initialRotation.Y + _maxY * Mathf.Pow(_shake, 2) * GetNoiseFromSeed(1)),
(float)(_initialRotation.Z + _maxZ * Mathf.Pow(_shake, 2) * GetNoiseFromSeed(2)));
}
public void AddShake(float shakeAmount)
{
_shake = Mathf.Clamp(_shake + shakeAmount, 0.0, 1.0);
}
private double GetNoiseFromSeed(int seed)
{
_noise.Seed = seed;
return _noise.GetNoise1D((float)(_time * _noiseSpeed));
}
}

View File

@@ -0,0 +1 @@
uid://bb36q1wpe0tlw

View File

@@ -0,0 +1,20 @@
[gd_scene load_steps=3 format=3 uid="uid://didc6vnf5ftlg"]
[ext_resource type="Script" uid="uid://bb36q1wpe0tlw" path="res://src/camera/ShakeCamera.cs" id="1_ubmds"]
[sub_resource type="FastNoiseLite" id="FastNoiseLite_ubmds"]
frequency = 0.08
fractal_octaves = 4
[node name="Camera3D" type="Camera3D"]
cull_mask = 1048569
doppler_tracking = 1
fov = 52.0
near = 0.01
far = 9000.0
script = ExtResource("1_ubmds")
_shakeIntensity = 1.8
_maxX = 0.0
_maxY = 5.0
_noise = SubResource("FastNoiseLite_ubmds")
_noiseSpeed = 40.0