• math.Approach Issues
    2 replies, posted
Good day. I have been trying to make my entity's alpha fade out, and the code I currently have is here: [lua]hook.Add("Think", "ChangeAlpha", function() for k,v in pairs(ents.GetAll()) do if v:GetNWBool("Skeleton") == true then local r,g,b,a = v:GetColor() if a <= 0 then v:Remove() else Alpha1 = math.Approach(a, 0, 0.6) v:SetColor(r, g, b, Alpha1) end end end end)[/lua] However, the transition is far too fast. To change the rate, I need to change the third number in the math.Approach. For some reason, it does not accept a number lower than 0.6 - otherwise it does not fade out at all. Any idea why? Is there an alternative I can use? Am I doing something wrong? Thanks in advance.
Color is stored as integers, this means that if you set the alpha of the entity to something like 254.5, it will be rounded to 255. And it will never fade. math.Approach in this situation is a rather bad idea, I'd suggest using CurTime instead: [lua] -- it takes 1 second to fade from 255 to 0 local FadeTime = 1 hook.Add("Think", "ChangeAlpha", function() for k,v in pairs(ents.GetAll()) do if v:GetNWBool("Skeleton") then local r,g,b,a = v:GetColor() if not v.NextAlphaFade then v.NextAlphaFade = CurTime() + FadeTime * a / 255 else a = math.Clamp(255 * (v.NextAlphaFade - CurTime()) / FadeTime, 0, 255) end if a <= 0 then v:Remove() else v:SetColor(r, g, b, a) end else v.NextAlphaFade = nil end end end) [/lua] Remember that every entity has its own table, so you can feel free to store anything you want in there. [editline]02:51PM[/editline] Someone else wanted to do something similar a while ago, if you have trouble understanding how CurTime() works in there, have a look at this post: [url]http://www.facepunch.com/showpost.php?p=22527363&postcount=9[/url]
Thanks for the reply, very informative! I never knew about the integer being rounded. Thanks.
Sorry, you need to Log In to post a reply to this thread.