Note: this plugin is C# only.
- Easy to use API
- Predefined shake algorithms
- Easily extensible
- Supports both 2D and 3D
clone this repo:
git clone https://github.com/Hyrdaboo/DwarfImpulse
or you can also download zip by clicking Code>Download ZIP
- After completing the installation, navigate to the downloaded files, and you will find the addons folder.
- Drag and drop this folder into the root directory of your project.
- If your project already has an addons folder, simply merge the contents of the addons folder from the downloaded project with your existing addons folder.
The most basic form of camera shake is done using Noise. There's a built-in preset NoiseShake
that handles noise based camera shake.
Usage example:
using Godot;
using DwarfImpulse;
public partial class ShakeController : Camera3D
{
[Export] private ShakeDirector3D shakeDirector;
[Export] private FastNoiseLite noise;
public override void _Process(double delta)
{
if (Input.IsActionJustPressed("ui_accept"))
{
shakeDirector.Shake(NoiseShake.CreateWithNoise(noise)
.WithDuration(3.0f)
.WithEulersAmount(new Vector3(0.05f, 0, 0.03f)));
}
}
}
Warning
Placing ShakeDirector in Autoload is advised against but if you need to do so use the ShakeDirector(bool isGlobal)
constructor
and set it to true. This will ensure the node always stays in the root scene.
-
Add this script to a Node3D, and then initialize its parameters.
-
Assign the camera as the target for ShakeDirector3D
-
Press play and when you press space the camera shake should initiate
Note: You could also do these steps from code but this way it's more visual
Sometimes, when more precise shakes are required, NoiseShake
may not be suitable. In such cases, you can use the BounceShake
preset, and the usage is almost identical.
Example:
// start the shakeDirector with BounceShake this time and set a start direction vector along which the target will bounce
shakeDirector.Shake(new BounceShake()
.WithDuration(0.3f)
.WithStartDir(new Vector3(1, 1, 0))
.WithFrequency(5.0f));
There are a couple of demos you can take a look at and learn from.
- Inside the addons folder navigate to Samples>Scenes. Here you will find the four available demos.
- Setup the input map for the demos:
![](https://private-user-images.githubusercontent.com/67780454/294784443-de3ac431-155f-4765-931c-5a2d615de80e.png?jwt=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJnaXRodWIuY29tIiwiYXVkIjoicmF3LmdpdGh1YnVzZXJjb250ZW50LmNvbSIsImtleSI6ImtleTUiLCJleHAiOjE3Mzk1NzUxNTEsIm5iZiI6MTczOTU3NDg1MSwicGF0aCI6Ii82Nzc4MDQ1NC8yOTQ3ODQ0NDMtZGUzYWM0MzEtMTU1Zi00NzY1LTkzMWMtNWEyZDYxNWRlODBlLnBuZz9YLUFtei1BbGdvcml0aG09QVdTNC1ITUFDLVNIQTI1NiZYLUFtei1DcmVkZW50aWFsPUFLSUFWQ09EWUxTQTUzUFFLNFpBJTJGMjAyNTAyMTQlMkZ1cy1lYXN0LTElMkZzMyUyRmF3czRfcmVxdWVzdCZYLUFtei1EYXRlPTIwMjUwMjE0VDIzMTQxMVomWC1BbXotRXhwaXJlcz0zMDAmWC1BbXotU2lnbmF0dXJlPTcxMGQ5ZWNjODI3YTI2ZDVmNmU1Mzc1ZmJiYTNjNDg0MTBjMGVjYWRmMmU3MmI2ZTZkNmJlMzA3NTNmMDk0NDImWC1BbXotU2lnbmVkSGVhZGVycz1ob3N0In0.v07vbGznV4R3HagFqlwyyTZv8kpbbnugqrBCD_Rof7o)
You can also define your own ShakePresets. Any class that inherits the abstract class ShakePreset
can be used with ShakeDirector3D
or ShakeDirector2D
class MyCustomShake : ShakePreset
{
public MyCustomShake WithDuration(float duration)
{
DurationLeft = duration;
return this;
}
// this method is called every frame while the shake is active
internal override Displacement ExecuteShake(float delta)
{
// return a new displacement with 0 positional offset and random rotation
return new Displacement(Vector3.Zero, new Vector3(0.05f * (GD.Randf() * 2 - 1), 0, 0.03f * (GD.Randf() * 2 - 1)));
}
}
Usage:
shakeDirector.Shake(new MyCustomShake().WithDuration(3.0f));
We don't want shakes to start and end abruptly. Time Envelope addresses this by controlling the strength of Shakes over time. You can take a look at the interactive demonstration by gasgiant
Example usage:
var preset = NoiseShake.CreateWithNoise(noise)
.WithOffsetAmount(new Vector3(25, 25, 0))
.WithEnvelope(new Envelope(10, 1, 0.6f, Degree.Quadratic))
.WithDuration(1.0f);
shakeDirector.Shake(preset);
In cases where we need to have a camera shake originate from a 3D source we can make use of Spatial Attenuation. It controls the strength of shakes over distance relative to a shake source. You can take a look at the interactive demonstration by gasgiant
Example usage:
var preset = NoiseShake.CreateWithNoise(noise)
.WithDuration(1.0f)
.WithScrollSpeed(1000) //the speed at which the noisemap is scrolled (basically frequency)
.WithEnvelope(new Envelope(10.0f, 2.0f, 0.35f, Degree.Quadratic))
.WithEulersAmount(shakeRotationAmount)
.WithOffsetAmount(shakeOffsetAmount);
preset.SpatialAttenuation.ShakeSourceMinDistance = minDistance; // min distance is where the strength of the shake is at max
preset.SpatialAttenuation.ShakeSourceMaxDistance = maxDistance; // shake strength reaches 0 at max distance
preset.SpatialAttenuation.ShakeSource = GlobalPosition; // the global position of the shake source in 3D world coordinates
shakeDirector.Shake(preset);
This project was inspired by gasgiant