Hello everrrrone.
Hänge seit längerem an einem kleinen Problem, ich möchte ein Flugzeug in der Luft spawnen und geradeaus fliegen lassen.
Geht eigentlich auch alles, nur das "ich kann das Flugzeug DIREKT geradeaus fliegend starten" ist der problematische Teil.
Flugzeug wird erstellt, Pilot wird reingesetzt und clientseitig mit den benötigten Control-Funktionen ausgestattet damit er so lange es geht in der Luft bleibt.
(Ja, jedes Flugzeug wird anders darauf reagieren(z.B. Rustler - Beagle) - trotzdem sollte der Start irgendwie funktionieren)
setElementVelocity mehr als genug ausprobiert aber da es ja die X, Y, Z Richtungsangaben sind wird das Fahrzeug nicht geradeaus sondern nur nach Nord-Ost-Süd-West geschossen.
setVehicleTurnVelocity ist das gleiche, Welt-Richtungsangabe.
setElementSpeed(useful function) genauso getestet aber das klappt bei meinem Vorhaben auch nicht wirklich weiter(benötigt ohnehin einen Timer) da ich eigentlich eine Offset-Richtungsangabe für das Fahrzeug bräuchte(X - Links&Rechts, Y - Vorne&Hinten, Z - Hoch&Runter).
Bitte keine Vorschläge mit moveObject & attachElements, das habe ich bereits gemacht aber ich möchte diesen Weg erstmal ausarbeiten.
(moveObject und danach detachElements bringt nichts, da wird das Flugzeug einfach ohne Movement 'gedroppt' und fällt runter)
Wie kann ich das mit ElementVelocity lösen?
Code
--Useful Function
function getElementSpeed(theElement, unit)
-- Check arguments for errors
assert(isElement(theElement), "Bad argument 1 @ getElementSpeed (element expected, got " .. type(theElement) .. ")")
local elementType = getElementType(theElement)
assert(elementType == "player" or elementType == "ped" or elementType == "object" or elementType == "vehicle" or elementType == "projectile", "Invalid element type @ getElementSpeed (player/ped/object/vehicle/projectile expected, got " .. elementType .. ")")
assert((unit == nil or type(unit) == "string" or type(unit) == "number") and (unit == nil or (tonumber(unit) and (tonumber(unit) == 0 or tonumber(unit) == 1 or tonumber(unit) == 2)) or unit == "m/s" or unit == "km/h" or unit == "mph"), "Bad argument 2 @ getElementSpeed (invalid speed unit)")
-- Default to m/s if no unit specified and 'ignore' argument type if the string contains a number
unit = unit == nil and 0 or ((not tonumber(unit)) and unit or tonumber(unit))
-- Setup our multiplier to convert the velocity to the specified unit
local mult = (unit == 0 or unit == "m/s") and 50 or ((unit == 1 or unit == "km/h") and 180 or 111.84681456)
-- Return the speed by calculating the length of the velocity vector, after converting the velocity to the specified unit
return (Vector3(getElementVelocity(theElement)) * mult).length
end
function setElementSpeed(element, unit, speed)
if (unit == nil) then unit = 0 end
if (speed == nil) then speed = 0 end
speed = tonumber(speed)
local acSpeed = getElementSpeed(element, unit)
if (acSpeed~=false) then -- if true - element is valid, no need to check again
local diff = speed/acSpeed
if diff ~= diff then return end -- if the number is a 'NaN' return end.
local x,y,z = getElementVelocity(element)
setElementVelocity(element,x*diff,y*diff,z*diff)
return true
end
return false
end
function getPositionFromElementOffset(element,offX,offY,offZ)
local m = getElementMatrix(element)
local x = offX * m[1][1] + offY * m[2][1] + offZ * m[3][1] + m[4][1]
local y = offX * m[1][2] + offY * m[2][2] + offZ * m[3][2] + m[4][2]
local z = offX * m[1][3] + offY * m[2][3] + offZ * m[3][3] + m[4][3]
return x, y, z
end
addCommandHandler("pl",
function(lp)
local x, y, z = getElementPosition(lp)
local px, py, pz = x+math.random(0, 50), y+math.random(0, 50), z
local angle = ( 360 - math.deg ( math.atan2 ( ( x - px ), ( y - py ) ) ) ) % 360
local Plane = createVehicle(getVehicleModelFromName("Rustler"), x, y, z+40, 0, 0, angle, "FLYING")
local Pilot = createPed(183, x, y, z+42)
warpPedIntoVehicle(Pilot, Plane)
createBlipAttachedTo(Plane)
setElementFrozen(Plane, true)
setTimer(
function()
setTimer(
function()
setElementSpeed(Plane, "mph", 200) --Mit Timer, ansonsten wird keine Bewegung erkannt
--setElementVelocity(Plane, 0, 0, 0.01)
end, 200, 1)
setElementFrozen(Plane, false)
end, 500, 1)
triggerClientEvent(root, "planeMode", Plane, Pilot)
end)
Alles anzeigen
local function planeMode(ped)
local veh = source
if isElement(veh) and getElementType(veh) == "vehicle" then
if ped then
setPedControlState(ped, "horn", true) --Räder/Landegklappen einziehen(funktioniert nicht)
setPedAnalogControlState(ped, "accelerate", 0.7) --Gas
setPedAnalogControlState(ped, "steer_back", 0.07) --Bei dem mieserablen Start zuerst hochziehen
setTimer(setPedAnalogControlState, 2000, 1, ped, "steer_back", 0.01) --Für einen normalen Flug nur noch sehr leicht
end
end
end
addEvent("planeMode", true)
addEventHandler("planeMode", root, planeMode)
Alles anzeigen