How to tween visibility property smoothly for Node2D/Sprite2D? [Shaders]

Godot Version

v4.2.2.stable.official

Question

(UPD1: Please, look at reply below, where we found that problem with shaders which doesn’t give a permission to change modulate:a property)

Well, I have an enemy scene (CharacterBody2D) which has:

I wanna make it appear/disappear when it comes close to the player’s character. All is fine with this stuff except animation smoothness.. It changes visibility like from true to false (nothing tweening). I’ve tried those methods:

  1. Tween’s method:
class_name GhostEnemy
extends Enemy

@onready var visuals = $Visuals

var is_visibility_on := false


func visibility_on():
	add_to_group("enemy")
	is_visibility_on = true

	var tween: Tween = create_tween()
	tween.tween_property(visuals, "modulate:a", 1, 2)


func visibility_off():
	remove_from_group("enemy")
	is_visibility_on = false

	var tween: Tween = create_tween()
	tween.tween_property(visuals, "modulate:a", 0, 2)

  1. Animation Player’s method:

Maybe I’ve done something wrong and it would be great if somebody helps, please :wink:

I just tested your tweening method and it works fine.
Maybe the problem is that you’re setting GhostEnemy.visible = false somewhere in your code, but it looks like it has nothing to do with your tweening.

Where are you executing visibility_off? can we get some context?

You’re totally right! problem was with shaders… ops… my bad, I don’t know why I decided to ask a question without testing tween on another sprites, sorry, and thanks for help)

Problem was with my HitFlashComponent (where is my shader) which regulates sprite’s color
To be honest, it’s my first experience with gdshaders, I’m bad at this)

Well, full scene looks like this:

You can use animation player to make it invisible or visible its very easy to use.(With modulate)
As you need when it comes closer to player you can make to play animation at that time.

Yeah, you’re right, problem wasn’t with tween, it’s cause of shader about which I completely forgot. Now I’m trying to fix it (look at my reply above)

Ah i am completely beginner at that point( i have used animation player to fade out,thats why i could help you with this)so with shaders i cant help you,sorry😅

Anyway, thanks)

Are you doing that with a shader? Then why don’t you do it ALL with the shader?
I feel like doing it half-code and half-shader will get you unexpected results if you don’t handle that correctly.

Just define a uniform called “alpha” or “alpha_uniform” or something and set the value there. Keep in mind it has to be a value between 0 and 1.
Then in your shader, line 12, just do this:

COLOR = vec4(final_color.r, final_color.g, final_color.b, alpha_uniform)

Yeah, I’ve already done it, thanks for your help! :grin: