So I’ve got a system to spawn a plane with a ped and set the ped into the vehicle, then tell the ped to fly to a certain location. But after the plane is spawned it will just slowly fall and soon after the ped decides to nose dive the plane into the ground and doesn’t seem to be trying to pull up. I can hear the engines of the plane, before it explodes so he’s trying something. Below is the code if anyone wants to take a crack. I’d assume it’s because I’m trying a more server authoritative design so clients are not allowed to spawn objects/peds, so I have to rely on the server to task AI which doesn’t seem to work as good as I’d hoped.
Server Side
function StartAirDropWIP(dropPos)
Citizen.CreateThread(function()
--Get a random heading
local rHeading = math.random(0, 360)+0.0
--How far the plane spawns from the drop coord
local spawnDistance = 1500.0
--Theta of random heading
local theta = (rHeading/180) * 3.14
--Get the planes spawn
local rPlanespawn = vector3(dropPos.x, dropPos.y, dropPos.z) - vector3(math.cos(theta) * spawnDistance, math.sin(theta) * spawnDistance, -500)
local dx = dropPos.x - rPlanespawn.x
local dy = dropPos.y - rPlanespawn.y
headingCB = nil
--Find closest player to drop pos to do the calc
local plyDist = -1
local closestPly = nil
for k,v in pairs(GetPlayers()) do
if DoesEntityExist(GetPlayerPed(v)) then
local dist = #(GetEntityCoords(GetPlayerPed(v)) - rPlanespawn)
if plyDist == -1 or dist < plyDist then
plyDist = dist
closestPly = v
end
end
end
if closestPly == nil then
closestPly = GetPlayers()[1]
end
--Trigger client event to do a calculation for server
TriggerClientEvent("fivez:GetHeadingFromVector", closestPly, dx, dy)
while headingCB == nil do
Citizen.Wait(1)
end
--local heading = GetHeadingFromVector_2d(dx, dy)
--Create the plane that will drop the crate
--IDEA: spawn under the closest player to properly spawn the plane then TP it to spawn proper position
local plane = CreateVehicle(GetHashKey("alkonost"), rPlanespawn.x, rPlanespawn.y, rPlanespawn.z, headingCB, true, true)
while not DoesEntityExist(plane) do
Citizen.Wait(1)
end
FreezeEntityPosition(plane, true)
TriggerClientEvent("fivez:AirdropPlane", -1, NetworkGetNetworkIdFromEntity(plane), dropPos.x, dropPos.y)
SetEntityHeading(plane, headingCB)
SetVehicleDoorsLocked(plane, 2)
SetEntityDistanceCullingRadius(plane, 50000.0)
--Create the ped driver that will be told where to go
local closestPlyCoords = GetEntityCoords(GetPlayerPed(closestPly))
local driver = CreatePed(0, GetHashKey("s_m_m_pilot_02"), rPlanespawn.x, rPlanespawn.y, rPlanespawn.z+2.5, 0.0, true, true)
FreezeEntityPosition(driver, true)
while not DoesEntityExist(driver) do
Citizen.Wait(1)
end
Entity(driver).state.human = 1
SetEntityDistanceCullingRadius(driver, 50000.0)
SetPedIntoVehicle(driver, plane, -1)
--TaskWarpPedIntoVehicle(driver, plane, -1)
TriggerClientEvent("fivez:AirdropPilot", -1, NetworkGetNetworkIdFromEntity(driver))
--Go to drop pos, keep z same as start position so ped doesn't nose dive
--TaskGoToCoordAnyMeans(driver, dropPos.x, dropPos.y, 500.0, 120.0, 0.0, true, 0.0, 0.0)
TaskGoStraightToCoord(driver, dropPos.x, dropPos.y, rPlanespawn.z, 120.0, -1, headingCB, 50000.0)
--TaskGoStraightToCoord(driver, vector3(dropPos.x, dropPos.y, dropPos.z) + vector3(0.0, 0.0, 500.0), 60.0, -1, headingCB, 0.0)
--TaskVehicleDriveToCoord(driver, plane, dropPos.x, dropPos.y, 1500.0, 120.0, 1.0, GetEntityModel(plane), 16777216, 1.0, 1.0)
FreezeEntityPosition(driver, false)
FreezeEntityPosition(plane, false)
--Wait till plane is in range of drop position
local dist = #(GetEntityCoords(plane) - vector3(dropPos.x, dropPos.y, rPlanespawn.z))
while dist > 100 do
dist = #(GetEntityCoords(plane) - vector3(dropPos.x, dropPos.y, rPlanespawn.z))
Citizen.Wait(1)
end
--Get plane coords to spawn the air drop to
local planeCoords = GetEntityCoords(plane)
TriggerClientEvent("fivez:AirdropComplete", -1)
--Tell pilot to fly away
TaskGoStraightToCoord(driver, dropPos.x + 5000.0, dropPos.y + 5000.0, rPlanespawn.z, 120.0, -1, headingCB, 0.0)
--TaskVehicleDriveToCoord(driver, plane, -5000, -5000, 1500.0, 120.0, 1.0, GetEntityModel(plane), 16777216, 1.0, 1.0)
--Create the crate to drop, place it below the plane so it doesn't cause any issues with it
local dropCrate = CreateObject(GetHashKey("prop_box_wood02a_pu"), planeCoords.x, planeCoords.y, planeCoords.z-2.0, true, true, false)
while not DoesEntityExist(dropCrate) do
Citizen.Wait(1)
end
FreezeEntityPosition(dropCrate, false)
--Let the crate free fall for a second in a half
Citizen.Wait(1500)
FreezeEntityPosition(dropCrate, true)
--Get the crate position
local crateCoords = GetEntityCoords(dropCrate)
--Create a parachute to attach to the crate to stop it from falling as quick
local parachute = CreateObject(GetHashKey("p_cargo_chute_s"), crateCoords.x, crateCoords.y, crateCoords.z, true, true, false)
while not DoesEntityExist(parachute) do
Citizen.Wait(1)
end
TriggerClientEvent("fivez:AirdropAttachParachute", -1, NetworkGetNetworkIdFromEntity(parachute), NetworkGetNetworkIdFromEntity(dropCrate))
FreezeEntityPosition(dropCrate, false)
--Wait until the drop crate hits the ground
while DoesEntityExist(dropCrate) do
Citizen.Wait(50)
end
--Get the parachutes position
crateCoords = GetEntityCoords(parachute)
--Delete the parachute
DeleteEntity(parachute)
--Create the actual loot crate
local lootCrate = CreateObject(GetHashKey("ex_prop_adv_case"), crateCoords.x, crateCoords.y, crateCoords.z, true, true, false)
while not DoesEntityExist(lootCrate) do
Citizen.Wait(1)
end
TriggerClientEvent("fivez:AddNotification", -1, "WIP Airdrop has touched ground! Find and loot it!")
return
end)
end
Client Side
RegisterNetEvent("fivez:AirdropPlane", function(planeNetId)
local entity = NetworkGetEntityFromNetworkId(planeNetId)
if DoesEntityExist(entity) then
SetEntityDynamic(entity, true)
ActivatePhysics(entity)
SetVehicleForwardSpeed(entity, 120.0)
SetHeliBladesFullSpeed(entity)
SetVehicleEngineOn(entity, true, true, false)
ControlLandingGear(entity, 3)
planeEntity = entity
end
end)
RegisterNetEvent("fivez:AirdropPilot", function(driverNetId)
local entity = NetworkGetEntityFromNetworkId(driverNetId)
if DoesEntityExist(entity) then
SetPedIntoVehicle(entity, plane, -1)
SetBlockingOfNonTemporaryEvents(entity, false)
SetPedKeepTask(entity, true)
SetPlaneMinHeightAboveTerrain(planeEntity, 500.0)
--TaskPlaneMission(entity, planeEntity, 0, 0, tonumber(dropPosX), tonumber(dropPosY), 502.0, 4, 0, 0, GetEntityHeading(planeEntity), 505.0, 500.0)
--TaskVehicleDriveToCoord(entity, planeEntity, vector3(tonumber(dropPosX)+0.0, tonumber(dropPosY)+0.0, 600.0), 120.0, 1.0, GetHashKey("alkonost"), 16777216, 1.0, true)
print("Set task")
driverEntity = entity
end
end)