Jump to content

Mounted Vehicle Weapons


Recommended Posts

Hello!

I have this code (Ace_Gambit is the original creator of it) for mounting weapons onto a vehicle's passenger side for shooting that I got from an old topic. The thing is, when I try to use it, it doesn't work. Here are all the files:

rocket_client.lua

local root = getRootElement()
local player = getLocalPlayer()
 
local toggleVehicleWeapon = false
local previousTick = getTickCount()
local projectileType = 21
 
function updateVehicleWeapon(source, dataName)
if (getElementType(source) ==  "vehicle" and dataName == "toggleVehicleWeapon") then
   toggleVehicleWeapon = (getElementData(source, dataName) and isPedInVehicle(player))
end
end
 
function callbackDataChange(dataName)
 updateVehicleWeapon(source, dataName)
end
 
function callbackVehicleEnter(thePlayer, seat)
 updateVehicleWeapon(source, "toggleVehicleWeapon")
end
 
function callbackRender()
if (getControlState("vehicle_fire") and toggleVehicleWeapon) then
if (getTickCount() > (previousTick + 150) and previousTick > 0) then
     fireProjectile()
end
end
end
 
function fireProjectile()
local vehicle = getPedOccupiedVehicle(player)
local gun = false
local gX, gY, gZ = 0, 0, 0
local gRotX, gRotY, gRotZ = 0, 0, 0
local pX, pY, pZ = 0, 0, 0
local vX, vY, vZ = 0, 0, 0
local currentLoSOffset = 3.5
local zPosOffset = 1.1
local thrust = 2.0
 previousTick = 0
if (vehicle) then
if (#getAttachedElements(vehicle) > 0) then
     gun = getAttachedElements(vehicle)[1]
if (gun) then
       gX, gY, gZ = getElementPosition(gun)
       gRotX, gRotY, gRotZ = getElementRotation(gun)
       pX = gX - math.sin(-math.rad(gRotZ + 90)) * currentLoSOffset
       pY = gY + math.cos(-math.rad(gRotZ + 90)) * currentLoSOffset
       pZ = gZ + zPosOffset
       vX = math.sin (math.rad((gRotZ + 90))) * thrust
       vY = math.cos(math.rad((gRotZ + 90))) * thrust
       vZ = 0
createExplosion(pX, pY, pZ, 5, true, -1.0, false)
createProjectile(player, projectileType, pX, pY, pZ, 0, nil, 0, 0, 0, vX, vY, vZ)
end
end
end
 previousTick = getTickCount()
end
 
function changeProjectileType(commandName, arg)
 projectileType = tonumber(arg) or 21
end
 
addEventHandler("onClientElementDataChange", root, callbackDataChange, true)
addEventHandler("onClientVehicleEnter", root, callbackVehicleEnter, true)
addEventHandler("onClientRender", root, callbackRender, true)
 
addCommandHandler("guntype", changeProjectileType)

rocket_server.lua

local guns =
{
[470] =
{
["xPosOffset"] = 1.0,
["yPosOffset"] = 0,
["zPosOffset"] = 0
}
}
 
function attachMod(playerSource, commandName)
local vehicle = getPedOccupiedVehicle(playerSource)
local gun = false
if (vehicle) then
   gun = guns[getElementModel(vehicle)] or false
if (gun) then
attachElements(createObject(2985, 0, 0, -100, 0, 0, 0), vehicle, gun.xPosOffset, gun.yPosOffset, gun.zPosOffset, 0, 0, 90)
setElementData(vehicle, "toggleVehicleWeapon", true)
end
end
end
 
function detachMod(playerSource, commandName)
local vehicle = getPedOccupiedVehicle(playerSource)
if (vehicle) then
for _, element in ipairs(getAttachedElements(vehicle)) do
destroyElement(element)
end
setElementData(vehicle, "toggleVehicleWeapon", false)
end
end
 
addCommandHandler("mount", attachMod, false)
addCommandHandler("unmount", detachMod, false)

meta.xml

<meta>
   <info author="Ace_Gambit" version="1.0" type="script"/>
   <script src="rocket_server.lua" />
   <script src="rocket_client.lua" type="client" />
</meta>

Can someone please identify the problem?

Thanks in advance.

Link to comment

I tested the resource, and it works. The thing is, it work only in Patriot.

local guns =
{
[470] =
{
["xPosOffset"] = 1.0,
["yPosOffset"] = 0,
["zPosOffset"] = 0
}
}

Is the definition of allowed vehicle ids (Patriot is 470), with position of the minigun on it. You can simply allow more vehicles, by providing their ids.

Link to comment
  • 11 months later...

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