Jump to content

[QUESTION] Vehicles "driving" on water


NovaCrew

Recommended Posts

local speed = 20
local buoyancy = 1.5
local drag = 1

function swimVehicle ( )
	local x, y, z = getElementPosition(vehicle)
	local waterZ = getWaterLevel ( x, y, z )
	if waterZ and z < waterZ then
		setElementData(vehicle, "buoyancy", buoyancy)
		setElementData(vehicle, "vehicleDrag", drag)
		setElementData(vehicle, "centerOfMass", {0,0,-1})
		setVehicleHandling(vehicle, "engineAcceleration", speed)
	else
		setElementData(vehicle, "buoyancy", 0)
		setElementData(vehicle, "vehicleDrag", 0)
		setVehicleHandling(vehicle, "engineAcceleration", 0)
	end
end
addEventHandler("onClientRender", root, swimVehicle)

Please note that this is just a basic example, you would need to fine tune the values and add more functionality to make it more realistic and functional. Also, this example is for client-side script, you need to adapt it for server-side.


 

Link to comment
16 hours ago, Trust aka Tiffergan said:
local speed = 20
local buoyancy = 1.5
local drag = 1

function swimVehicle ( )
	local x, y, z = getElementPosition(vehicle)
	local waterZ = getWaterLevel ( x, y, z )
	if waterZ and z < waterZ then
		setElementData(vehicle, "buoyancy", buoyancy)
		setElementData(vehicle, "vehicleDrag", drag)
		setElementData(vehicle, "centerOfMass", {0,0,-1})
		setVehicleHandling(vehicle, "engineAcceleration", speed)
	else
		setElementData(vehicle, "buoyancy", 0)
		setElementData(vehicle, "vehicleDrag", 0)
		setVehicleHandling(vehicle, "engineAcceleration", 0)
	end
end
addEventHandler("onClientRender", root, swimVehicle)

Please note that this is just a basic example, you would need to fine tune the values and add more functionality to make it more realistic and functional. Also, this example is for client-side script, you need to adapt it for server-side.


 

well, I really don't know what to type there xd (it's that server-side script)

spacer.png

Edited by NovaCrew
  • Confused 1
Link to comment
  • 2 weeks later...
addEventHandler("onVehicleEnter", root, function(player, seat)
	if source:getModel() == 416 then
		setElementData(source, "canDriveOnWater", true)
	end
end)

addEventHandler("onVehicleExit", root, function(player, seat)
	if source:getModel() == 416 and getElementData(source, "canDriveOnWater") then
		setElementData(source, "canDriveOnWater", false)
		setVehicleHandling(source, "dragCoeff", 0.2)
	end
end)

addEventHandler("onClientPreRender", root, function()
	for i, vehicle in ipairs(getElementsByType("vehicle")) do
		if getElementData(vehicle, "canDriveOnWater") then
			local x, y, z = getElementPosition(vehicle)
			if getWaterLevel(x, y, z) > z then
				setVehicleOnGround(vehicle)
				setVehicleHandling(vehicle, "dragCoeff", 10)
			else
				setVehicleHandling(vehicle, "dragCoeff", 0.2)
			end
		end
	end
end)

not tested tho

Link to comment
addEventHandler("onVehicleEnter", root, function(player, seat)
	if source:getModel() == 416 then
		setElementData(source, "canDriveOnWater", true)
	end
end)

addEventHandler("onVehicleExit", root, function(player, seat)
	if source:getModel() == 416 and getElementData(source, "canDriveOnWater") then
		setElementData(source, "canDriveOnWater", false)
		setVehicleHandling(source, "dragCoeff", 0.2)
	end
end)

addEventHandler("onVehicleMove", root, function()
	local vehicle = source
	if getElementData(vehicle, "canDriveOnWater") then
		local x, y, z = getElementPosition(vehicle)
		if getWaterLevel(x, y, z) > z then
			setVehicleOnGround(vehicle)
			setVehicleHandling(vehicle, "dragCoeff", 10)
		else
			setVehicleHandling(vehicle, "dragCoeff", 0.2)
		end
	end
end)

My apologies I did a mistake with the event, I’ve replaced it with my onVehicleMove now it should work

literally sometimes I forget it’s for MTA 

addEventHandler("onVehicleEnter", root, function(player, seat)
	if getElementModel(source) == 416 then
		setElementData(source, "canDriveOnWater", true)
	end
end)

addEventHandler("onVehicleExit", root, function(player, seat)
	if getElementModel(source) == 416 and getElementData(source, "canDriveOnWater") then
		setElementData(source, "canDriveOnWater", false)
		if getVehicleHandling(source)["dragCoeff"] then
			setVehicleHandling(source, "dragCoeff", 0.2)
		end
	end
end)

addEventHandler("onVehicleMove", root, function()
	local vehicle = source
	if getElementData(vehicle, "canDriveOnWater") then
		local x, y, z = getElementPosition(vehicle)
		local waterZ = getWaterLevel(x, y, z)
		if waterZ and waterZ > z then
			setElementPosition(vehicle, x, y, waterZ)
			if getVehicleHandling(vehicle)["dragCoeff"] then
				setVehicleHandling(vehicle, "dragCoeff", 10)
			end
		else
			if getVehicleHandling(vehicle)["dragCoeff"] then
				setVehicleHandling(vehicle, "dragCoeff", 0.2)
			end
		end
	end
end)

 

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