Jump to content

setElementSpeed() not really setting the speed


KALIL

Recommended Posts

As I wrote in the title, the setElementSpeed() function doesn't appear to change anything ecxept the getElementSpeed() result, here's the function:

function brrr ( source )
  if getElementModel ( source ) == 96 then
    outputChatBox(getElementSpeed(source, 1)) --returns 30
    setElementSpeed(source, 1, 100)
    outputChatBox(getElementSpeed(source, 1)) --returns 100 (to check if the speed has changed)
    wait(1)
    outputChatBox(getElementSpeed(source, 1)) --returns 100 (to check if it stays constant after 1 sec)
    wait(2)
    outputChatBox(getElementSpeed(source, 1)) --returns 100 (to check if it stays constant after 3 secs)
  end
end

here's the ingame chat:

kbcrWBb.png

 

 

 

 

 

here's the whole code:

function wait(seconds)
    local start = os.time()
    repeat until os.time() > start + seconds
end


function getElementSpeed(theElement, unit)
    -- Check arguments for errors
    assert(isElement(theElement), "Bad argument 1 @ getElementSpeed (element expected, got " .. type(theElement) .. ")")
    local elementType = getElementType(theElement)
    assert(elementType == "player" or elementType == "ped" or elementType == "object" or elementType == "vehicle" or elementType == "projectile", "Invalid element type @ getElementSpeed (player/ped/object/vehicle/projectile expected, got " .. elementType .. ")")
    assert((unit == nil or type(unit) == "string" or type(unit) == "number") and (unit == nil or (tonumber(unit) and (tonumber(unit) == 0 or tonumber(unit) == 1 or tonumber(unit) == 2)) or unit == "m/s" or unit == "km/h" or unit == "mph"), "Bad argument 2 @ getElementSpeed (invalid speed unit)")
    -- Default to m/s if no unit specified and 'ignore' argument type if the string contains a number
    unit = unit == nil and 0 or ((not tonumber(unit)) and unit or tonumber(unit))
    -- Setup our multiplier to convert the velocity to the specified unit
    local mult = (unit == 0 or unit == "m/s") and 50 or ((unit == 1 or unit == "km/h") and 180 or 111.84681456)
    -- Return the speed by calculating the length of the velocity vector, after converting the velocity to the specified unit
    return (Vector3(getElementVelocity(theElement)) * mult).length
end




function setElementSpeed(element, unit, speed)
    local unit    = unit
    local speed   = speed
	local acSpeed = getElementSpeed(element, unit)
	if acSpeed and acSpeed~=0 then 
		local diff = speed/acSpeed
		if diff ~= diff then return false end
        	local x, y, z = getElementVelocity(element)
		return setElementVelocity(element, x*diff, y*diff, z*diff)
	end
	return false
end





function brrr ( source )
  if getElementModel ( source ) == 96 then
    outputChatBox(getElementSpeed(source, 1)) --returns 30
    setElementSpeed(source, 1, 100)
    outputChatBox(getElementSpeed(source, 1)) --returns 100 (to check if the speed has changed)
    wait(1)
    outputChatBox(getElementSpeed(source, 1)) --returns 100 (to check if it stays constant after 1 sec)
    wait(2)
    outputChatBox(getElementSpeed(source, 1)) --returns 100 (to check if it stays constant after 3 secs)
  end
end

function bindz (source)
  bindKey ( source, "Z", "down", brrr )
end

function unbindz (source)
  unbindKey ( source, "Z", "down", brrr )
end

function binds (source)
  bindKey ( source, "space", "down", brrr )
end

function unbinds (source)
  unbindKey ( source, "space", "down", brrr )
end

function checkkey ()
  bindKey ( source, "space", "down", bindz )
  bindKey ( source, "space", "up", unbindz )
  bindKey ( source, "z", "down", binds )
  bindKey ( source, "z", "up", unbinds )
end

addEventHandler ("onPlayerSpawn", root, checkkey)

 

Link to comment
8 hours ago, xLive said:

Welcome to the forums @KALIL. Your thread has been moved to the Scripting section. Please use this section for scripting questions in the future.

Thanks!

 

10 hours ago, -ffs-PLASMA said:

Player peds walking speed is defined by its animation, you cannot change it via setElementSpeed.

thanks for the info, I tried to replace the sprint animation but it doesn't seem to work, what do i need to change here?

IFP = engineLoadIFP( "sprint.ifp", "test" )
engineReplaceAnimation( localPlayer, "ped", "sprint_civi", "test", "fast_sprint" )

 

Link to comment

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
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...