Godot Version
Godot -version 4
Question
I want to change the color of a sprite from lets say red (#FF0000) to blue (#0000FF) over time, i am up to trying any method, but if i can do this through modulate that would be great
-thanks!
Godot -version 4
I want to change the color of a sprite from lets say red (#FF0000) to blue (#0000FF) over time, i am up to trying any method, but if i can do this through modulate that would be great
-thanks!
Try this?
modulate = lerp(modulate, target_color, delta*10.0)
Or,
time += delta
time = clampf(time, 0.0, 1.0)
modulate = lerp(modulate, target_color, time)
With this above codes, you will able to customize the speed of changing color over time.
Hey thanks so much for the previous help!, but i ran into a new issue, if i wanted to change the color from red to blue, instead of reducing the red value to 0 it just adds the blue value resulting with purple
extends Node2D
@onready var sprite_2d: Sprite2D = $Sprite2D
var speed: float = 0.02 # Speed of transition
var target_color_red: Color = Color(255, 0, 0)
var target_color_blue: Color = Color(1, 0, 255)
var target_color_black: Color = Color(1, 0, 0)
var time = 0
func _process(delta: float) -> void:
time += 0.01
if time <= 3:
sprite_2d.modulate = sprite_2d.modulate.lerp(target_color_red, speed * delta)
elif time >= 3 && time <= 9:
sprite_2d.modulate = sprite_2d.modulate.lerp(target_color_blue, speed * delta)
print(sprite_2d.modulate)
But why you don`t done the same, as of my second codes. Your codes ie different.
Try this:
create_tween().tween_property(self, "modulate", Color.BLUE, 1)
Otherwise you can store the Tween
in a variable to set a callback or other things:
var t: Tween = create_tween()
t.tween_property(self, "modulate", Color.BLUE, 1)
You can of course change color and speed (last parameter) as you want
If you want to do something when the animation has finished, you can do it like this:
var t: Tween = create_tween()
t.tween_property(self, "modulate", Color.BLUE, 1)
await t.finished
print("Finished!")
Here’s the Tween
’s documentation:
It can do muuuch more than changing colors so consider using it when you have to change a parameter continuously over time