[RESOLVED] Tween node mysteriously speeding up

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

I’ve got a little shader effect - just a sine wave controlling vertex scale so far - and I’ve got a freq uniform controlling it. I’m using a tween to control this in gdscript.

The obj starts at freq zero (nothing happening), then tweens to freq one. When it’s done it fires a signal and tweens back from freq one to zero:

Here’s the tween subroutine where I send in the start and stop values:

func run_tween(a, b):
	printt("freq", mat.get_shader_param("freq"))
	var t = $Tween
							mat, "shader_param/freq", a, b, 1, 
							Tween.TRANS_BOUNCE, Tween.EASE_IN_OUT)

Here’s signal method:

func _on_Tween_tween_all_completed():
	if not odd_even:
		run_tween(1, 0)
	printt("freq", mat.get_shader_param("freq"))
	odd_even = not odd_even

Obviously I have the odd_even bool there to go back and forth.

This prints:

freq	0
freq	1
freq	1
freq	0

Except the frequency is visibly speeding up every time you run it. Same duration, quicker sine wave. It even happens when you lerp without easing.

I’m not changing any other values. Am I missing something obvious here?

Sorry, I was being an idiot as usual. The shader had TIME in the sine function which obviously grew over time making the frequency higher. Nothing to do with the tween.

DaddyMonster | 2022-07-23 12:28

Glad you found/fixed the issue - thanks for the follow-up here.

For forum housekeeping, I’d suggest that you copy your comment as an Answer and then select it as Best. That’ll show (on the main board) that the question 1) has an answer and 2) the answer resolved the problem - which is potentially important when searching the forum for help…

jgodfrey | 2022-07-23 14:29

That’s a good idea, let me do that.

DaddyMonster | 2022-07-23 15:26

:bust_in_silhouette: Reply From: DaddyMonster

In case anyone else has the same issue. The problem was with the shader code, I had:

void vertex() {
	VERTEX *= sin(freq*TIME) * amp * 0.5 + 1.;

Specifically the sin(freq*TIME) part. TIME grows over, well, time. Normally if you put it inside a sine function it doesn’t matter whether TIME is equal to 10 or 20,000 as the rate is the same so the sine wave is the same.

But once I multiplied by freq i changed that. I was bringing the value from 0-10 or 0-10,000 over the same period of time. That meant the animation would get quicker over time depending on how long it’s been since the game started.

The solution was just to drop TIME, in my case I scaled it up slightly with a new uniform float which I set to 10.0 to get the effect I wanted.

void vertex() {
	VERTEX *= sin(freq*rate) * amp * 0.5 + 1.;

Now, it always travels between 0 and 10.