Jump to content

Trying to toggleAllControls to off and then binding new keys issue


Recommended Posts

So I'm trying to make a simple platformer race thing with platform game controls (A and D = left and right, W = jump)

To do this I first toggleAllControls to false and then use bindKey to do what I want with my keys (control acceleration and set the vehicle Z rotation to 90 or -90)

The problem I'm finding is it only works sometimes.

The intended functionality is that when you press D the car moves in a straight line along the X axis, when you press A the car moves in a straight line on the same axis but in the opposite direction.

What sometimes happens is the toggleAllControls doesn't seem to be working properly so the car will also slide along the Y axis.

As you can see I've tried fiddling with the order of the functions (using timers to try an better understand what could be happening)... but to no avail.

-- client

function startup()
	setTimer( function()
		toggleAllControls (false, true, false)
		toggleControl ( "vehicle_left", false ) 
		toggleControl ( "vehicle_right", false ) 
	end, 1000, 1)
	setTimer( function()
		keys()
		invincible()
		outputChatBox("GO!")
	end, 2000, 1)
end
addEventHandler("onClientResourceStart", getResourceRootElement(getThisResource()), startup)

function invincible()
	local veh = getPedOccupiedVehicle(localPlayer)
	setVehicleDamageProof(veh, true)
end

function leftRight ( key, keyState )
	local veh = getPedOccupiedVehicle(localPlayer)
	local rx, ry, rz = getElementRotation(veh)
	if key == "d" then
		if keyState == "down" then
			setControlState ( localPlayer, "accelerate", true )
			setElementRotation(veh, rx, ry, 90)
		elseif keyState == "up" then
			setControlState ( localPlayer, "accelerate", false )
		end
	elseif key == "a" then
		if keyState == "down" then
			setControlState ( localPlayer, "accelerate", true )
			setElementRotation(veh, rx, ry, -90)
		elseif keyState == "up" then
			setControlState ( localPlayer, "accelerate", false )
		end
	end
end

function keys ( commandName )
	bindKey( "d", "down", leftRight )
	bindKey( "d", "up", leftRight )
	bindKey( "a", "down", leftRight )
	bindKey( "a", "up", leftRight )
	bindKey( "w", "down", jumpy )
end

function onClientRender()
	local x, y, z = getElementPosition(localPlayer)
	setCameraMatrix(x, y+10, z, x, y, z, 0, 100)
end
addEventHandler("onClientRender", root, onClientRender)

function jumpy()
	local veh = getPedOccupiedVehicle(getLocalPlayer())
	if (veh) then
		if isVehicleOnGround(veh) == true then
			doubleJump = true
			local sx,sy,sz = getElementVelocity(veh)
			setElementVelocity (veh, sx, sy, 0.3)
		elseif isVehicleOnGround(veh) == false and doubleJump then
			doubleJump = false
			local sx,sy,sz = getElementVelocity(veh)
			setElementVelocity (veh, sx, sy, 0.3)
		end
	end
end

Any ideas what I'm doing wrong?

Link to comment
  • Moderators
25 minutes ago, [FOTL]Aphex said:

Any ideas what I'm doing wrong?

 

Not exactly haha, but I can give you some (bad) advice. How about using some brute force for a starter? (First make it, then optimise it)

function adjustVehicle()
	local veh = getPedOccupiedVehicle(localPlayer)
	if not veh then return end
	local rx, ry = getElementRotation(veh)
	if getKeyState("d") then
		setControlState(localPlayer, "accelerate", true)
		setElementRotation(veh, rx, ry, 90)
	elseif getKeyState("a") then
		setControlState(localPlayer, "accelerate", true)
		setElementRotation(veh, rx, ry, -90)
	else
		setControlState(localPlayer, "accelerate", false)
	end
end

addEventHandler("onClientPreRender", root, adjustVehicle)

 

 

 

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