How to apply shake effect to select nodes?

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By exuin

In my game, I want to the player to be able to interact with objects and have a dialogue box pop up with character portraits and stuff. I also want to be able to apply a shaking effect to the dialogue box. The dialogue box is on a separate canvas layer than the player. I tried applying a shaking effect to the Camera2D under the player node, but that just shook the player and the background instead of the stuff in the CanvasLayer. I tried adding a Camera2D as a child of the dialogue box, but when I made it current, the camera was pointed at the origin, so it didn’t align with the player’s current position.

How do I apply a shaking effect to the dialogue box while still showing the player’s current position in the background?

:bust_in_silhouette: Reply From: njamster

Just use an AnimationPlayer or Tween to change either the offset-property of the whole CanvasLayer or the position-/rect_position-property of your dialogue box. Here’s an code example for using a Tween to shake the CanvasLayer:

extends Node2D

var tween_values = [0.0, 20.0]

func _enter_tree():
	var tween = Tween.new()
	tween.name = "Tween"
	add_child(tween)    
	tween.connect("tween_completed", self, "on_tween_completed")

func _ready():
	start_tween()

func start_tween():
	$Tween.interpolate_property($CanvasLayer, "offset:y", tween_values[0], tween_values[1], 1.0)
	$Tween.start()

func on_tween_completed(object, key):
	tween_values.invert()
	start_tween()

Thanks, using an AnimationPlayer worked.

exuin | 2020-03-24 00:02

1 Like