Jump to content

Move Vehicle Door According To Cursor


Z4Zy

Recommended Posts

Hello!

      I created a very basic script of vehicle door control with cursor. When press "1", driver's door open/close according to the cursor move. When press "2" that event will remove.

 

Meta.xml

<meta>
	<script src="client.lua" type="client" />
</meta>

 

Client.lua


local dState = false

function moveDoor()

  local vehicle = getPedOccupiedVehicle(localPlayer)
  
	if (getVehicleDoorOpenRatio(vehicle, 2) >= 1) then
		dState = "close"
	elseif (getVehicleDoorOpenRatio(vehicle, 2) <= 0) then
		dState = "open"
	end
  
	if (dState == "open") then
		setVehicleDoorOpenRatio(vehicle, 2, getVehicleDoorOpenRatio(vehicle, 2) + 0.01 )
		
	elseif (dState == "close") then
		setVehicleDoorOpenRatio(vehicle, 2, getVehicleDoorOpenRatio(vehicle, 2) - 0.01 )
	end
	
end


bindKey("1", "up",
function ()
	addEventHandler("onClientCursorMove", root, moveDoor)
end)

bindKey("2", "up",
function ()
	removeEventHandler("onClientCursorMove", root, moveDoor)
end)

 

 

I need your help with two problems.

 

Problem - 01

The door should move only if player move the cursor left and right on the screen. Not for moving up and down.

 


 

Problem - 02

Door movement direction (open/close) need to be depend on players view position.

Meaning, If player's camera at the front of the vehicle, then when player move the cursor to right, door will open.

If player's camera at the back of the vehicle, then when player move the cursor to right, door will close.

This will apply vice versa to left.

 

  • Simple Image Demonstration
Spoiler

 

  Front

7sXBFt9.png

 

   Back

kmJyQkp.png

 

 

Appreciate any help.

Thank In Advance !!

 

Link to comment

Hi.

I've put together a code for you in which the door movement works as expected, there is one up and down movement involved at a certain camera/vehicle angle (see the pictures below).

If you don't need that up/down movement either, we can remove it.

Spoiler

spacer.pngspacer.png

local cameraElement = getCamera()
local oldCursorRelX = 0.5
local oldCursorRelY = 0.5
local moveSensitivity = 2

function moveDoor(cursorRelX, cursorRelY)
	if not isCursorShowing() then
		return
	end

	local vehicleElement = getPedOccupiedVehicle(localPlayer)
	if not isElement(vehicleElement) then
		return
	end

	local cameraMatrix = getElementMatrix(cameraElement)
	local vehicleMatrix = getElementMatrix(vehicleElement)

	local dotProductX = cameraMatrix[1][1] * vehicleMatrix[1][1] + cameraMatrix[1][2] * vehicleMatrix[1][2]
	local dotProductY = cameraMatrix[3][1] * vehicleMatrix[1][1] + cameraMatrix[3][2] * vehicleMatrix[1][2]

	local cursorDeltaX = (cursorRelX - oldCursorRelX) * moveSensitivity
	local cursorDeltaY = (cursorRelY - oldCursorRelY) * moveSensitivity

	oldCursorRelX = cursorRelX
	oldCursorRelY = cursorRelY

	local dotProductCombined = cursorDeltaX * -dotProductX + cursorDeltaY * dotProductY
	local openRatio = getVehicleDoorOpenRatio(vehicleElement, 2) + dotProductCombined

	if openRatio < 0 then
		openRatio = 0
	elseif openRatio > 1 then
		openRatio = 1
	end

	setVehicleDoorOpenRatio(vehicleElement, 2, openRatio)
end

bindKey("f1", "down",
	function ()
		addEventHandler("onClientCursorMove", root, moveDoor)
	end
)

bindKey("f2", "down",
	function ()
		removeEventHandler("onClientCursorMove", root, moveDoor)
	end
)

 

  • Thanks 1
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...