Skip to content

Instantly share code, notes, and snippets.

@DjPale
Created January 3, 2017 22:46
Show Gist options
  • Select an option

  • Save DjPale/10976c705a38287761f734ec5b2f82ea to your computer and use it in GitHub Desktop.

Select an option

Save DjPale/10976c705a38287761f734ec5b2f82ea to your computer and use it in GitHub Desktop.
Godot Shake Node
extends Node
export(NodePath) var target_path
export(float) var duration = 0.2
export(Vector2) var amplitude = Vector2(5.0, 5.0)
var target = null
var fx_timer = 0
func _ready():
if target_path != null && !target_path.is_empty():
target = get_node(target_path)
else:
target = get_parent()
if target != null && target extends Node2D:
set_process(true)
else:
printerr(get_name() + ": node is null or not Node2D - disabled")
func _process(delta):
if fx_timer <= 0: return
fx_timer -= delta
if fx_timer < 0: fx_timer = 0
shake_tween(fx_timer)
func shake_tween(t):
var s_t = t / duration
var scaled_shake = Vector2(s_t * amplitude.x, s_t * amplitude.y)
var random_shake = Vector2(rand_range(-scaled_shake.x, scaled_shake.x), rand_range(-scaled_shake.y, scaled_shake.y))
target.set_pos(random_shake)
func shake():
if target == null: return
target.set_pos(Vector2())
fx_timer = duration
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment