Jump to content

setElementVelocity


Recommended Posts

local sprint = true

function updateSprint ()
local state = getControlState ( "forwards" )
local playeR = getLocalPlayer()
local speedx, speedy, speedz = getElementVelocity (playeR)
	if state and sprint == false then 
    setElementVelocity(playeR, speedx * 2, speedy * 2, speedz) 
	elseif sprint == true then 
	return 
end
end
addEventHandler ( "onClientElementStreamIn", getRootElement(), updateSprint )

function startSprint()
	if sprint then
		sprint = false
		else
		sprint = true
	end
end
bindKey ( "mouse2", "down", startSprint )

im not even sure im going the right way about this but its not seeming to work :/

Link to comment

It's pretty close but you need to use onClientRender instead of streamIn.

instead of using getLocalPlayer() just use localPlayer it is a predefined variable for example x,y,a = getElementVelocity(localPlayer)

This wont work unless you jump because you can't set a ped's velocity while it is on the ground.

easiest way is to just get the pisition to the front of the player and teleport him to it but the distance to the position in front of you should be very small like .01

Here's a simpler way of writing the function.

addEventHandler ( "onClientRender", root,
    function ()
        if getKeyState( "mouse2" ) then
            -----------
            --CODE
            -----------
        end
    end
)

 

Link to comment
vel =.8

function sPeedUp()
	if isElement(element) then
		if getElementData(element, "charging") == "on" then
			local x, y, z = getElementPosition(element)
			local prot = getPedRotation(element)
			local nx, ny = getPointFromDistanceRotation(x, y, vel, (prot)*-1)
			local clear = isLineOfSightClear( x, y, z, nx, ny, z, true, true, true, true, true, true, true)
			if clear == true then
				local lx, ly = getPointFromDistanceRotation(x, y, 1, (prot-8)*-1)
				local rx, ry = getPointFromDistanceRotation(x, y, 1, (prot+8)*-1)
				local clearl = isLineOfSightClear( x, y, z, lx, ly, z, true, true, true, true, true, true, true)
				local clearr = isLineOfSightClear( x, y, z, rx, ry, z, true, true, true, true, true, true, true)
				if clearl == true and clearr == true then
					local task = getPedSimplestTask(element)
					if task == "TASK_SIMPLE_PLAYER_ON_FOOT" then
						local nz = getGroundPosition ( nx, ny, z+1 )
						if  getDistanceBetweenPoints3D( x, y, z, nx, ny, nz) < 2 then
						setElementPosition(element, nx, ny, nz+1, false)
						end
					end
				end
			end
		end
	end
end
addEventHandler("onClientRender", root, sPeedUp)

 

  • Like 1
Link to comment

well ive ran into a few more problems trying to make it work better. Im not sure how you made it teleport in front of the player, im trying to get it to work for all keys WSAD, also when i jump and im moving forward sometimes it still teleports me back on the ground

function startSprint()
local wep = getPedWeaponSlot (localPlayer)
if not (wep == 0) then return end
	if sprint then
		sprint = false
		setElementData(localPlayer, "charging","on")
		else
		sprint = true
		setElementData(localPlayer, "charging","off")
	end
end
bindKey ( "mouse2", "down", startSprint )

vel = 0.05

function sPeedUp()
	if isElement(localPlayer) then
		if getElementData(localPlayer, "charging") == "on" then
			local x, y, z = getElementPosition(localPlayer)
			local prot = getPedRotation(localPlayer)
			local nx, ny = getPointFromDistanceRotation(x, y, vel, (prot)*-1)
			local clear = isLineOfSightClear( x, y, z, nx, ny, z, true, true, true, true, true, true, true)
			if clear == true then
				local lx, ly = getPointFromDistanceRotation(x, y, 1, (prot-8)*-1)
				local rx, ry = getPointFromDistanceRotation(x, y, 1, (prot+8)*-1)
				local clearl = isLineOfSightClear( x, y, z, lx, ly, z, true, true, true, true, true, true, true)
				local clearr = isLineOfSightClear( x, y, z, rx, ry, z, true, true, true, true, true, true, true)
				if clearl == true and clearr == true then
				    local W = getControlState ( "forwards" )
				    local S = getControlState ( "backwards" )
				    local A = getControlState ( "left" )
				    local D = getControlState ( "right" )
					local jump = getControlState ( "jump" )
					if jump then return end
					local nz = getGroundPosition ( nx, ny, z+1 )
				    if  getDistanceBetweenPoints3D( x, y, z, nx, ny, nz) < 2 then
					if W or S or A or D then
						setElementPosition(localPlayer, nx, ny, nz+1, false)
						end
					end
				end
			end
		end
	end
end
addEventHandler("onClientRender", root, sPeedUp)

 

Link to comment
1 hour ago, ViRuZGamiing said:

Wouldn't get camera matrix' 4th, 5th and 6th returned float help?

I suppose that Position is always where you're looking at of you'd need to Ignore the height of where it's looking else you'd teleport on top of things. At least that's what I think

"else you'd teleport on top of things" - this only happens when i aim my weapon and im teleporting forward into a light pole, so yes it happens but, other than that i do not teleport ontop of things. not much of a point in speeding up the player while aiming anyways

"I suppose that Position is always where you're looking at" - not exactly, W works perfectly fine in any direction  however moving left right and backwards still moves me forwards. im assuming it has something to do with getPedRotation

"wouldn't get camera matrix' 4th, 5th and 6th returned float help?" - i only need the direction of the player not the camera

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...