Sign in to follow this  

[FUNC] easeGameSpeed

Recommended Posts

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:


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

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")
		if X == ease.targetSpeed then
			ease.isActive = false
			if type(ease.callback) == "function" then

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

^ Paste this code somewhere in your script file ^


Example Usage

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
                            easeGameSpeed(1.0, 1000) end, 1500, 1)

                    --OR instantly start the reversal back to 1.0:
                    easeGameSpeed(1.0, 1500)


-- And obviously the function somewhere down here..


Edited by Infra

Share this post

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

  • Recently Browsing   0 members

    No registered users viewing this page.