Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

How do I make the color of my smoke fade from its color to a new random color?

Asked by 9 years ago

I am trying to script a color changing smoke but it either rapidly changes, goes over 255 or under 0 and then doesn't change back, is there any way to make it so it smoothly changes color without it flashing or going over 255 or under 0? This is the script I was using:

number1 = math.random()
number2 = math.random()
number3 = math.random()

while wait() do
    script.Parent.Smoke1.Smoke.Color = Color3.new(number1,number2,number3)
    script.Parent.Smoke2.Smoke.Color = Color3.new(number1,number2,number3)
    number1 = math.random()
    number2 = math.random()
    number3 = math.random()
end

Is there any way to make it kind of fade to random colors or is this the best I can do?

1 answer

Log in to vote
1
Answered by
Goulstem 8144 Badge of Merit Moderation Voter Administrator Community Moderator
9 years ago

What You Have To Do

You have to make a Color3 Tweener. This is a difficult concept to explain but I want to give you code so you can study it because I don't wanna leave you empty handed.

Basically what you have to do is get the current color, and the wanted color. Then find the difference in individual RGB values betwixt the two, and gradually change the color according to the differences.


Code

function TweenColor3(Color1, Color2, Output, Time, Smoothness)
    --Find the difference between the current color and the next color
    local Difference = {
            R = Color2.r - Color1.r, --Red difference
            G = Color2.g - Color1.g, --Green difference
            B = Color2.b - Color1.b --Blue difference
    }
    --Gradually change color according to the difference
    for i = 1, Time * Smoothness do
        Output = Color3.new(
            Output.r + Difference.R * (Time / Smoothness),
            Output.g + Difference.G * (Time / Smoothness),
            Output.b + Difference.B * (Time / Smoothness)
        )
        wait()
    end
end 

--[[
    Usage;

    local Color = Color3.new(255,0,0)
    local wantedColor = Color3.new(0,255,0)
    local outputColor = Color3.new()
    local Time = 6

    TweenColor3(Color,wantedColor,outputColor,Time,15)
]]

For Your Use

function TweenColor3(Color1, Color2, Output, Time, Smoothness)
    local Difference = {
            R = Color2.r - Color1.r,
            G = Color2.g - Color1.g,
            B = Color2.b - Color1.b
    }
    for i = 1, Time * Smoothness do
        Output = Color3.new(
            Output.r + Difference.R * (Time / Smoothness),
            Output.g + Difference.G * (Time / Smoothness),
            Output.b + Difference.B * (Time / Smoothness)
        )
        wait()
    end
end 

--[[
    Usage;

    local output = script.Parent.Smoke.Smoke1.Smoke
    local a = math.random --make it shorter haha
    local wantedColor = Color3.new(a(0,255)/255,a(0,255)/255,a(0,255)/255)
    local Time = 6

    TweenColor3(output.Color,wantedColor,output,Time,15)
]]
0
I have tried to use this, for calling the function I used: TweenColor3(script.Parent.Smoke1.Smoke.Color,Color3.new(math.random(0,255),math.random(0,255),math.random(0,255)),script.Parent.Smoke1.Smoke.Color,5) -- Is this correct?? I was confused. General_Scripter 425 — 9y
0
It won't work automatically, you're going to have to set 'Output' on line 10 to whatever you want to change. Goulstem 8144 — 9y
0
I did put script.Parent.Smoke.Smoke1.Smoke.Color General_Scripter 425 — 9y
0
I'm pretty sure you have to divide the values in your second argument (the random color) by 255 as Color3s have a range between 0 and 1. Spongocardo 1991 — 9y
View all comments (2 more)
0
Worked with editing but no vote up because I had to edit and it's copied from kingkiller1000 Color3 Tween script without credit. General_Scripter 425 — 9y
0
Just because it might look similiar to someone else's doesn't mean it's copied(: There's not that broad a spectrum for ways to do this, you know. But i'm glad it worked for you. Goulstem 8144 — 9y
Ad

Answer this question