Threaded View


  1. 02-05-2019, 21:59
    #1

    Banned
    69 • 148 • 0
    In-game: dnlk<>

    easeGameSpeed
    This function changes the players game speed step by step (that way it appears smoother)
    I'm using this in my maps a lot and in my opinion it makes a huge difference


    Difference between setGameSpeed and easeGameSpeed:



    Code:
    local ease = {}
    
    function easeGameSpeed(targetSpeed, overTime, callback)
    	ease.startedAt 		= 	getTickCount()
    	ease.startSpeed		= 	getGameSpeed()
    	ease.targetSpeed 	= 	math.round(targetSpeed, 4, "floor")
    	ease.overTime 		= 	overTime
    	ease.callback		=	(type(callback) == "function" or type(callback) == "nil") and (type(callback) == "function" and callback or nil) or nil
    	ease.isActive 		= 	true
    end
    
    addEventHandler("onClientRender", root, function()
    	if ease.isActive then
    	    local progress = (getTickCount() - ease.startedAt) / ((ease.startedAt + ease.overTime) - ease.startedAt)
    	    local currentSpeed = math.round(getGameSpeed(), 1, (ease.startSpeed > ease.targetSpeed and "ceil" or "floor"))
    
    		local X, Y, Z = interpolateBetween(currentSpeed, 0.0, 0.0, ease.targetSpeed, 0.0, 0.0, progress, "InOutQuad")
    		X = math.round(X, 4, (ease.startSpeed > ease.targetSpeed and "floor" or "ceil"))
    		if ease.startSpeed > ease.targetSpeed and X <= ease.targetSpeed then
    			X = math.round(X, 1, "ceil")
    		elseif ease.startSpeed < ease.targetSpeed and X >= ease.targetSpeed then
    			X = math.round(X, 1, "floor")
    		end
    		
    		setGameSpeed(X)
    		if X == ease.targetSpeed then
    			ease.isActive = false
    			if type(ease.callback) == "function" then
    				ease.callback();
    			end
    		end
    	end
    end)
    
    function math.round(number, decimals, method)
        decimals = decimals or 0
        local factor = 10 ^ decimals
        if (method == "ceil" or method == "floor") then return math[method](number * factor) / factor
        else return tonumber(("%."..decimals.."f"):format(number)) end
    end
    ^ Paste this code somewhere in your script file ^






    Example Usage
    Code:
    local Marker = createMarker(X, Y, Z, "corona", SIZE, 0, 0, 0)
    
    addEventHandler("onClientMarkerHit", getRootElement(), function(playerid)
        if playerid == getLocalPlayer() then
            if isPedInVehicle(playerid) then
                local vehicleid = getPedOccupiedVehicle(playerid)
                
                if source == Marker then
                    -- Params:        (targetSpeed, overTime, callback)
                    easeGameSpeed(0.6, 1500, function()
                    	-- Target game speed reached
    
                    	-- Now you can either delay the reversal by using setTimer:
                    	-- This example waits 1500ms before it starts reversing the game speed back to 1.0 over a time of 1000ms
                    	setTimer(function()
                        	    easeGameSpeed(1.0, 1000) end, 1500, 1)
    
                    	--OR instantly start the reversal back to 1.0:
                    	easeGameSpeed(1.0, 1500)
                    end)
    
                end
            end
        end
    end)
    Last edited by dnlk; 08-05-2019 at 17:20. Reason: Function rewritten

1 Users reading this thread (0 Members and 1 Guests)