Tween shake does not exists anymore?

Godot Version

4.2.1

Question

Is there an alternative tween property to use for the shake?

I’ve got this ShakeComponent which shakes the sprite, but when I try to use it, it gives me the following error:

E 0:00:01:0457 shake_component.gd:14 @ tween_shake(): The tweened property “shake” does not exist in object “ShakeComponent:<Node#31004296503>”.
<C++ Error> Condition “!prop_valid” is true. Returning: nullptr
<C++ Source> scene/animation/tween.cpp:110 @ tween_property()
shake_component.gd:14 @ tween_shake()
enemy.gd:16 @ ()
hitbox_component.gd:15 @ _on_hurtbox_entered()

class_name ShakeComponent
extends Node

var current_shake = 0

@export var sprite: Node2D
@export var shake_amount: = 2.0
@export var shake_duration: = 0.4

func tween_shake():
	current_shake = shake_amount
	var tween = create_tween()
	tween.tween_property(self, "shake", 0.0, shake_duration).from_current()

func _physics_process(delta: float) -> void:
	sprite.position = Vector2(randf_range(-current_shake, current_shake), randf_range(-current_shake, current_shake))

I’m wondering if there is an alternative to use the shake property which would work

Node does not have a “shake” property, did you mean to edit the “current_shake” you have defined?

I want to shake a Sprite2D for the duration of shake_duration for x and y between - current_shake and current_shake.

You say a Node2D doesn’t have a shake property. Where could I read up on what nodes have what properties?

Pressing F1 will pull up a help search in Godot, you can read the docs on Node or Node2D to learn what properties it has. You could search for “shake” but nothing has that property, i suspect you mean to use “current_shake” as that variable is in your script.

You’re right, I clearly need to read up on how to use tweens and edit their properties. Thanks for the help!

Do you even need tweens?
You already have all the info you need. Is this what you’re going for?
I changed the amount to 5 so it’s more noticeable and so that when you press the space bar you start shaking the image.

If this is what you need, without the need for tweens, here’s the code:

class_name ShakeComponent
extends Node2D

var current_shake = 0

@export var sprite: Node2D
@export var shake_amount: = 5.0
@export var shake_duration: = 1


func _physics_process(delta: float) -> void:
	if Input.is_action_just_pressed("ui_accept") and current_shake == 0:
		current_shake = shake_amount
	
	current_shake -= shake_amount * delta / shake_duration
	if current_shake < 0:
		current_shake = 0
	
	sprite.position = Vector2(randf_range(-current_shake, current_shake), randf_range(-current_shake, current_shake))
	

1 Like

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.