• Simple Fade Out animation
    3 replies, posted
Hey! I'm looking for a better way to apply a Fade Out animation on an entity. I did something very basic that works when I press USE, but I'm sure there is a better way to do it. Here's my test code: [code]FADEOUT_SPEED = 1 PRESSED = false VALUE = 255 local function KeyPress(ply, key) entTest = ply:GetEyeTrace().Entity if SERVER and key == IN_USE then PRESSED = true end end hook.Add("KeyPress", "KeyPress", KeyPress) local function Think() if PRESSED then VALUE = VALUE - FADEOUT_SPEED entTest:SetRenderMode(RENDERMODE_TRANSALPHA) entTest:SetColor(Color(0,0,0, VALUE )) -- Color is not important to me if VALUE == 0 then PRESSED = false VALUE = 255 return end end end hook.Add("Think", "Test:Think", Think)[/code]
I see 4 problems: 1) Animations is FPS dependent ( not sure if you care ) 2) The fade out color is black instead of white. Normally all entities are while 3) You are using globals instead of variables assigned to the entity. This will break your system if it is applied to more than one entity at the same time. 4) Why not use PlayerUse hook?
1) Yes this is the problem I need to solve, but I don't know how. I really don't like to call the Think() hook, it's too expensive. 2) Yes I know it was just a test 3) Oh, understood, thx! 4) I call a KeyPress just for the test too, in the end I don't need to use it to do what I want to do. To clarify, I want to clean dropped entities on the ground after X seconds with ent:Remove(), but I want to do a Fade Out on the entity just before the removing, and this is it that I don't know to do.
You can use Lerp. Instead of forcing a value to be something and track it, then invert like cs weapon base ( is wrong ); you basically give it a set speed per second, so for it to fade 100% in 1 second you'd use -255 as the multiplier ( negative because we count down ). You'll also want to clamp. Here's something similar to lerp but it'll just give you the value to add to alpha... [code]local _fade = -1; -- Fade in or out? local _duration = 3; -- How long the fade should take, in seconds. local _value = FrameTime( ) * ( _duration / 255 ) * _fade; // You'll need to have _color set somewhere -- This will e _color.a = math.Clamp( _color.a + _value, 0, 255 );[/code] The same thing using Lerp: [code]local _fade = -1; -- Fade in or out? local _duration = 3; -- How long the fade should take, in seconds. // This returns a value from 0-1 local _modifier = Lerp( FrameTime( ) * ( _duration / 255 ) * _fade, _color.a / 255, ( _fade < 0 && 0 || 1 ) ); // You'll need to have _color set somewhere _color.a = _modifier * 255; [/code] That should be right.
Sorry, you need to Log In to post a reply to this thread.