Jump to content

[HELP]Mounted Machine Gun


HustraDev

Recommended Posts

hello

my last topic was closed beacuse topic bumping so i decided to create anthor one because i really need help

ihave a problem on Xwad mg Machine Gun script the problem is i can't mount the machine gun on a vehicle the code

client side :

    inUse = false
    inUse_s = false
    inUse_s2 = false
    inUse_s3 = false
    w, h = guiGetScreenSize ()




    function create_client(weapon,weapon2,weapon3)

    local x,y,z = getElementPosition (weapon)
    local weapon_c = createWeapon("minigun", x,y,z)
    setElementCollisionsEnabled( weapon_c, false )
    setWeaponState ( weapon_c, "ready" )
    setElementAlpha ( weapon_c, 255 )
    setWeaponFiringRate ( weapon_c , 60 )
    setElementParent( weapon_c, weapon )
    setWeaponProperty( weapon_c, "damage", 0)
    setWeaponProperty ( weapon_c, "fire_rotation", 0, -27, -5.5 )
    setWeaponProperty ( weapon_c, "weapon_range", 10000 )
    setWeaponProperty ( weapon_c, "target_range", 10000 )
    setWeaponClipAmmo( weapon_c, 250 )
    setElementData ( weapon_c, "owner", player )
    setElementData ( localPlayer, "gun", weapon_c )
    attachElements ( weapon_c, weapon, 1.1, -0.12, 0.152, -6, 3, -2 )


    local x,y,z = getElementPosition (weapon2)
    local weapon_c2 = createWeapon("minigun", x,y,z)
    setElementCollisionsEnabled( weapon_c2, false )
    setWeaponState ( weapon_c2, "ready" )
    setElementAlpha ( weapon_c2, 0 )
    setWeaponFiringRate ( weapon_c2 , 60 )
    setElementParent( weapon_c2, weapon2 )
    setWeaponProperty( weapon_c2, "damage", 0)
    setWeaponProperty ( weapon_c2, "fire_rotation", 0, -27, -5.5 )
    setWeaponProperty ( weapon_c2, "weapon_range", 10000 )
    setWeaponProperty ( weapon_c2, "target_range", 10000 )
    setWeaponClipAmmo( weapon_c2, 250 )
    setElementData ( weapon_c2, "owner", player )
    setElementData ( localPlayer, "gun2", weapon_c2 )
    attachElements ( weapon_c2, weapon2, 1.1, -0.12, 0.152, -6, 3, -2 )


    local x,y,z = getElementPosition (weapon3)
    local weapon_c3 = createWeapon("minigun", x,y,z)
    setElementCollisionsEnabled( weapon_c3, false )
    setWeaponState ( weapon_c3, "ready" )
    setElementAlpha ( weapon_c3, 0 )
    setWeaponFiringRate ( weapon_c3 , 60 )
    setElementParent( weapon_c3, weapon3 )
    setWeaponProperty( weapon_c3, "damage", 0)
    setWeaponProperty ( weapon_c3, "fire_rotation", 0, -27, -5.5 )
    setWeaponProperty ( weapon_c3, "weapon_range", 10000 )
    setWeaponProperty ( weapon_c3, "target_range", 10000 )
    setWeaponClipAmmo( weapon_c3, 250 )
    setElementData ( weapon_c3, "owner", player )
    setElementData ( localPlayer, "gun2", weapon_c3 )
    attachElements ( weapon_c3, weapon3, 1.1, -0.12, 0.152, -6, 3, -2 )
    end
    addEvent("createRealWeapon", true)
    addEventHandler("createRealWeapon",root, create_client)









    function enterMG()
    	if inUse == true then
    		setTimer ( exitMG, 100, 1)
    	else
    		if not isPedInVehicle(localPlayer) and inUse == false then
    			local text = getElementData ( localPlayer, "text" )
    			if isElement(text) then
    			inUse = true
    			setElementAlpha ( localPlayer, 0 )
    			triggerServerEvent ( "attachPlayerToMG", localPlayer)
    			addEventHandler('onClientRender', root, renderVehCross )
    			addEventHandler('onClientRender', root, updatePlayerWeapon )
    			toggleCockpitView()					
    			destroyElement(text)
    			local text2 = guiCreateLabel  ( 0.470, 0.32, 4, 2, "Press USE [G] to drop", true )
    			guiSetFont ( text2, "default-bold-small" )
    			setElementData (localPlayer, "text2", text2)
    			end 
    		end
    	end
    end
    bindKey ( "g", "down", enterMG)



    --1
    function inUse_server()
    inUse_s = true
    end
    addEvent("inUse_server", true)
    addEventHandler("inUse_server",getRootElement(), inUse_server)

    function noUse_server()
    inUse_s = false
    end
    addEvent("noUse_server", true)
    addEventHandler("noUse_server",getRootElement(), noUse_server)

    --2
    function inUse_server2()
    inUse_s2 = true
    end
    addEvent("inUse_server2", true)
    addEventHandler("inUse_server2",getRootElement(), inUse_server2)

    function noUse_server2()
    inUse_s2 = false
    end
    addEvent("noUse_server2", true)
    addEventHandler("noUse_server2",getRootElement(), noUse_server2)

    --3
    function inUse_server3()
    inUse_server3 = true
    end
    addEvent("inUse_server3", true)
    addEventHandler("inUse_server3",getRootElement(), inUse_server3)

    function noUse_server3()
    noUse_server3 = false
    end
    addEvent("noUse_server3", true)
    addEventHandler("noUse_server3",getRootElement(), noUse_server3)




    function exitMG()
    	inUse = false
    	isEnabled = false
    	setCameraTarget (localPlayer, localPlayer)
    	removeEventHandler ("onClientPreRender", root, updateCamera)
    	removeEventHandler ("onClientCursorMove", root, freecamMouse)
    	removeEventHandler('onClientRender', root, updatePlayerWeapon )		
    	removeEventHandler('onClientRender', root, renderVehCross )
    	triggerServerEvent("detachPlayerFromMG", localPlayer)
    	setElementAlpha ( localPlayer, 255 )
    	local text2 = getElementData ( localPlayer, "text2" )
    	destroyElement(text2)
    	local text = guiCreateLabel( 0.470, 0.32, 4, 2, "Press USE [G] to use", true)
    	guiSetFont ( text, "default-bold-small" )
    	setElementData (localPlayer, "text", text)
    	toggleControl ("fire", true)
    	toggleCockpitView()
    	end
    addEvent("getOutFromMacineGun", true)
    addEventHandler("getOutFromMacineGun",getRootElement(), exitMG)



    function onWasted ()
    	if inUse == true then
            inUse = false
            isEnabled = false
            setCameraTarget (localPlayer, localPlayer)
            removeEventHandler ( "onClientPreRender", root, updateCamera )
            removeEventHandler ( "onClientCursorMove", root, freecamMouse )
    		removeEventHandler ( 'onClientRender', root, updatePlayerWeapon )		
    		removeEventHandler ( 'onClientRender', root, renderVehCross )
    	    triggerServerEvent ( "detachPlayerFromMG", localPlayer )
    		setElementAlpha ( localPlayer, 255 )
    		toggleControl ("fire", true)
    		toggleCockpitView()
    		
    		local text = getElementData ( localPlayer, "text" )
    		local text2 = getElementData ( localPlayer, "text2" )
    		local img = getElementData ( localPlayer, "img" )
    		destroyElement(text)
    		destroyElement(text2)
    		destroyElement(img)
    	end
    end
    addEventHandler ( "onClientPlayerWasted", getLocalPlayer(), onWasted )

    function onDemage()
    	if inUse == true then
    		setPedAnimationProgress(localPlayer, "SilenceCrouchfire", 0.7)
    		triggerServerEvent ( "demage_anim", localPlayer)
    	end
    end
    addEventHandler ( "onClientPlayerDamage", getLocalPlayer(), onDemage )







    function renderVehCross ()
    	if inUse == true then
    		dxDrawImage (w/2-34, h/2-38,68,68,"files/crosshair.png",0,0,0)
    		setCameraFieldOfView("player",70)
    	else
    		removeEventHandler('onClientRender', root, renderVehCross )
    	end
    end

















    function fireCustomVehWeapon ()
    	if inUse == true then
    		local px,py,pz = getElementPosition (localPlayer)
    		local x,y,z = getWorldFromScreenPosition ( w/2, h/2, 50 )
    		setElementData ( localPlayer, "attachedToWeapon_aim", {x,y,z} )
    		setElementData ( localPlayer, "attachedToWeapon_fire", true)
    		setTimer ( updateWeaponTargetOnFire, 200, 1 )
    		triggerServerEvent("fire_server", localPlayer, localPlayer,px,py,pz)
    	end
    end
    bindKey ("mouse1", "down", fireCustomVehWeapon)


    function updateWeaponTargetOnFire ()
    if getKeyState ("mouse1") and inUse == true then

    local x,y,z = getWorldFromScreenPosition ( w/2, h/2, 75 )
    setElementData ( localPlayer, "attachedToWeapon_aim", {x,y,z} )

    setTimer ( updateWeaponTargetOnFire, 100, 1 )
    else
    setElementData ( localPlayer, "attachedToWeapon_fire", false)
    end
    end



    function fire_client(player,px,py,pz)
    		local sound_mg = getElementData ( localPlayer, "sound_mg" )
    		if not isElement(sound_mg) then
    			local gun = getElementData ( localPlayer, "gun" )
    			local aim = getElementData ( player, "attachedToWeapon_aim" )
    			local x,y,z = getElementPosition (localPlayer)
    			
    			local sound_mg = playSound3D ( "files/mg.wav", px,py,pz, true )
    			setSoundMaxDistance ( sound_mg, 500 )
    			setWeaponState ( gun, "firing" )
    			setWeaponTarget(gun, aim[1],aim[2],aim[3])
    			setElementData ( localPlayer, "sound_mg", sound_mg )
    			setTimer ( fireCustomWeapInTime, 50, 1, gun, player )
    		end
    		local sound_mg = getElementData ( localPlayer, "sound_mg" )
    		if not isElement(sound_mg) then
    			local gun = getElementData ( localPlayer, "gun2" )
    			local aim = getElementData ( player, "attachedToWeapon_aim" )
    			local x,y,z = getElementPosition ( gun )
    			local sound_mg = playSound3D ( "files/mg.wav", x,y,z, true )
    			setSoundMaxDistance ( sound_mg, 500 )
    			setWeaponState ( gun, "firing" )
    			setWeaponTarget(gun, aim[1],aim[2],aim[3])
    			setTimer ( fireCustomWeapInTime, 50, 1, gun, player )
    			setElementData ( localPlayer, "sound_mg", sound_mg )
    		end
    end
    addEvent("fire_client", true)
    addEventHandler("fire_client",getRootElement(), fire_client)

    function fireCustomWeapInTime (gun, player)
    	if isElement ( player ) and getElementData ( player, "attachedToWeapon_fire" ) then
    		local aim = getElementData ( player, "attachedToWeapon_aim" )
    		setWeaponTarget(gun, aim[1],aim[2],aim[3])
    		timer = setTimer ( fireCustomWeapInTime, 100, 1, gun, player )
    	else
    		local sound_mg = getElementData ( localPlayer, "sound_mg" )
    		local x,y,z = getElementPosition ( player )
    		setWeaponState ( gun, "ready" )
    		stopSound(sound_mg)
    	end
    end






    function fire_client2(player,px,py,pz)
    		local sound_mg2 = getElementData ( localPlayer, "sound_mg2" )
    		if not isElement(sound_mg2) then
    			local gun = getElementData ( localPlayer, "gun2" )
    			local aim = getElementData ( player, "attachedToWeapon_aim" )
    			local x,y,z = getElementPosition (localPlayer)
    			
    			local sound_mg2 = playSound3D ( "files/mg.wav", px,py,pz, true )
    			setSoundMaxDistance ( sound_mg2, 500 )
    			setWeaponState ( gun, "firing" )
    			setWeaponTarget(gun, aim[1],aim[2],aim[3])
    			setElementData ( localPlayer, "sound_mg2", sound_mg2 )
    			setTimer ( fireCustomWeapInTime2, 50, 1, gun, player )
    		end
    		local sound_mg2 = getElementData ( localPlayer, "sound_mg2" )
    		if not isElement(sound_mg2) then
    			local gun = getElementData ( localPlayer, "gun2" )
    			local aim = getElementData ( player, "attachedToWeapon_aim" )
    			local x,y,z = getElementPosition ( gun )
    			local sound_mg2 = playSound3D ( "files/mg.wav", x,y,z, true )
    			setSoundMaxDistance ( sound_mg2, 500 )
    			setWeaponState ( gun, "firing" )
    			setWeaponTarget(gun, aim[1],aim[2],aim[3])
    			setTimer ( fireCustomWeapInTime2, 50, 1, gun, player )
    			setElementData ( localPlayer, "sound_mg2", sound_mg2 )
    		end
    end
    addEvent("fire_client2", true)
    addEventHandler("fire_client2",getRootElement(), fire_client2)

    function fireCustomWeapInTime2 (gun, player)
    	if isElement ( player ) and getElementData ( player, "attachedToWeapon_fire" ) then
    		local aim = getElementData ( player, "attachedToWeapon_aim" )
    		setWeaponTarget(gun, aim[1],aim[2],aim[3])
    		timer = setTimer ( fireCustomWeapInTime2, 100, 1, gun, player )
    	else
    		local sound_mg2 = getElementData ( localPlayer, "sound_mg2" )
    		local x,y,z = getElementPosition ( player )
    		setWeaponState ( gun, "ready" )
    		stopSound(sound_mg2)
    	end
    end






    function fire_client3(player,px,py,pz)
    		local sound_mg3 = getElementData ( localPlayer, "sound_mg3" )
    		if not isElement(sound_mg3) then
    			local gun = getElementData ( localPlayer, "gun3" )
    			local aim = getElementData ( player, "attachedToWeapon_aim" )
    			local x,y,z = getElementPosition (localPlayer)
    			
    			local sound_mg3 = playSound3D ( "files/mg.wav", px,py,pz, true )
    			setSoundMaxDistance ( sound_mg3, 500 )
    			setWeaponState ( gun, "firing" )
    			setWeaponTarget(gun, aim[1],aim[2],aim[3])
    			setElementData ( localPlayer, "sound_mg3", sound_mg3 )
    			setTimer ( fireCustomWeapInTime3, 50, 1, gun, player )
    		end
    		local sound_mg3 = getElementData ( localPlayer, "sound_mg3" )
    		if not isElement(sound_mg3) then
    			local gun = getElementData ( localPlayer, "gun3" )
    			local aim = getElementData ( player, "attachedToWeapon_aim" )
    			local x,y,z = getElementPosition ( gun )
    			local sound_mg3 = playSound3D ( "files/mg.wav", x,y,z, true )
    			setSoundMaxDistance ( sound_mg3, 500 )
    			setWeaponState ( gun, "firing" )
    			setWeaponTarget(gun, aim[1],aim[2],aim[3])
    			setTimer ( fireCustomWeapInTime3, 50, 1, gun, player )
    			setElementData ( localPlayer, "sound_mg3", sound_mg3 )
    		end
    end
    addEvent("fire_client3", true)
    addEventHandler("fire_client3",getRootElement(), fire_client2)

    function fireCustomWeapInTime3 (gun, player)
    	if isElement ( player ) and getElementData ( player, "attachedToWeapon_fire" ) then
    		local aim = getElementData ( player, "attachedToWeapon_aim" )
    		setWeaponTarget(gun, aim[1],aim[2],aim[3])
    		timer = setTimer ( fireCustomWeapInTime3, 100, 1, gun, player )
    	else
    		local sound_mg3 = getElementData ( localPlayer, "sound_mg3" )
    		local x,y,z = getElementPosition ( player )
    		setWeaponState ( gun, "ready" )
    		stopSound(sound_mg3)
    	end
    end







    function stop_client()
    	if inUse == true then
    		local x,y,z = getElementPosition (localPlayer)
    		triggerServerEvent("ready_server", localPlayer, x,y,z)
    	end
    end
    bindKey ("mouse1", "up", stop_client)

    function stop(x,y,z)
    local sound_lastshoot = playSound3D ( "files/mg_lastshoot.wav", x,y,z )
    setSoundMaxDistance ( sound_lastshoot, 500 )
    end
    addEvent("ready_client", true)
    addEventHandler("ready_client",getRootElement(), stop)








    function createCol(posX,posY)
    local col = createColCircle ( posX+1,posY, 0.9 ) 

    addEventHandler ( "onClientColShapeHit", col, onHit )
    addEventHandler ( "onClientColShapeLeave", col, onLeave )
    end
    addEvent("createCol", true)
    addEventHandler("createCol",getRootElement(), createCol)

    function onHit( theElement )
    	if inUse_s == false then
    		if ( theElement == getLocalPlayer() ) and not isPedInVehicle(localPlayer) and inUse == false then
    			local text2 = getElementData ( localPlayer, "text2" )
    			if not isElement(text2) then
    			local text = guiCreateLabel( 0.470, 0.32, 4, 2, "Press USE [G] to use", true)
    			local img = guiCreateStaticImage(0.57, 0.3, 0.09, 0.06, "files/mg42.png", true)
    			guiSetFont ( text, "default-bold-small" )
    			setElementData (localPlayer, "text", text)
    			setElementData (localPlayer, "img", img)
    		end
    		end
    	end
    end

    function onLeave( theElement )
    	if ( theElement == getLocalPlayer() ) and not isPedInVehicle(localPlayer) and inUse == false then
    		local text = getElementData ( localPlayer, "text" )
    		local img = getElementData ( localPlayer, "img" )
    		destroyElement(text)
    		destroyElement(img)
    	end
    end








    function createCol2(posX,posY)
    local col2 = createColCircle ( posX+1,posY, 0.9 ) 

    addEventHandler ( "onClientColShapeHit", col2, onHit2 )
    addEventHandler ( "onClientColShapeLeave", col2, onLeave2 )
    end
    addEvent("createCol2", true)
    addEventHandler("createCol2",getRootElement(), createCol2)

    function onHit2(theElement)
    	if inUse_s2 == false then
    		if ( theElement == getLocalPlayer() ) and not isPedInVehicle(localPlayer) and inUse == false then
    			local text2 = getElementData ( localPlayer, "text2" )
    			if not isElement(text2) then
    			local text = guiCreateLabel( 0.470, 0.32, 4, 2, "Press USE [G] to use", true)
    			local img = guiCreateStaticImage(0.57, 0.3, 0.09, 0.06, "files/mg42.png", true)
    			guiSetFont ( text, "default-bold-small" )
    			setElementData (localPlayer, "text", text)
    			setElementData (localPlayer, "img", img)
    		end
    		end
    	end
    end

    function onLeave2(theElement)
    	if ( theElement == getLocalPlayer() ) and not isPedInVehicle(localPlayer) and inUse == false then
    		local text = getElementData ( localPlayer, "text" )
    		local img = getElementData ( localPlayer, "img" )
    		destroyElement(text)
    		destroyElement(img)
    	end
    end





    function createCol3(posX,posY)
    local col3 = createColCircle ( posX+1,posY, 0.9 ) 

    addEventHandler ( "onClientColShapeHit", col3, onHit3 )
    addEventHandler ( "onClientColShapeLeave", col3, onLeave3 )
    end
    addEvent("createCol3", true)
    addEventHandler("createCol3",getRootElement(), createCol3)

    function onHit3(theElement)
    	if inUse_s3 == false then
    		if ( theElement == getLocalPlayer() ) and not isPedInVehicle(localPlayer) and inUse == false then
    			local text2 = getElementData ( localPlayer, "text2" )
    			if not isElement(text2) then
    			local text = guiCreateLabel( 0.470, 0.32, 4, 2, "Press USE [G] to use", true)
    			local img = guiCreateStaticImage(0.57, 0.3, 0.09, 0.06, "files/mg42.png", true)
    			guiSetFont ( text, "default-bold-small" )
    			setElementData (localPlayer, "text", text)
    			setElementData (localPlayer, "img", img)
    		end
    		end
    	end
    end

    function onLeave3(theElement)
    	if ( theElement == getLocalPlayer() ) and not isPedInVehicle(localPlayer) and inUse == false then
    		local text = getElementData ( localPlayer, "text" )
    		local img = getElementData ( localPlayer, "img" )
    		destroyElement(text)
    		destroyElement(img)
    	end
    end










    function causeCustomWeaponDamage(hitElement)
    	if hitElement then
    		local owner = getElementData(source,"owner")
    				if getElementType ( hitElement ) == "vehicle" then
    					triggerServerEvent ( "causeVehicleDamageFromMG", localPlayer, hitElement ) 
    				end
    			end
    		end
    addEventHandler("onClientWeaponFire", getRootElement(), causeCustomWeaponDamage)


    function DamageToPlayersFromCustomWeapons(target)
        if target == localPlayer then
    		setElementHealth ( target, getElementHealth(target) - 8 )
        end
    end
    addEventHandler("onClientWeaponFire", root, DamageToPlayersFromCustomWeapons)







     













    function getPositionInfrontOfElement(x,y,rotation, meters)
        posX = x - math.sin(math.rad(rotation)) * meters
        posY = y + math.cos(math.rad(rotation)) * meters
        return posX, posY, posZ
    end

    function findRotation(x1,y1,x2,y2)
      local t = -math.deg(math.atan2(x2-x1,y2-y1))
      if t < 0 then t = t + 360 end;
      return t;
    end

    function getCameraRotation ()
        local px, py, pz, lx, ly, lz = getCameraMatrix()
        local rotz = 6.2831853071796 - math.atan2 ( ( lx - px ), ( ly - py ) ) % 6.2831853071796
        local rotx = math.atan2 ( lz - pz, getDistanceBetweenPoints2D ( lx, ly, px, py ) )
        rotx = math.deg(rotx)
        rotz = -math.deg(rotz)	
        return rotx, 180, rotz
    end

    function updatePlayerWeapon ()
    	if inUse == true then
    		local x, y, z, lx, ly, lz = getCameraMatrix ()
    		local rot = findRotation ( x,y,lx,ly )
    		local rot2_x, rot_y, rot2_z = getCameraRotation()
    		setElementRotation ( localPlayer, 0, 0, rot, "default", true )
    		triggerServerEvent ("rotatePlayerWeapon",localPlayer,rot,rot2_x)
    	end
    end















    local root = getRootElement()
    local localPlayer = getLocalPlayer()
    local PI = math.pi

    local isEnabled = false
    local wasInVehicle = isPedInVehicle(localPlayer)

    local mouseSensitivity = 0.1
    local rotX, rotY = 0,0
    local mouseFrameDelay = 0
    local idleTime = 2500
    local fadeBack = false
    local fadeBackFrames = 50
    local executeCounter = 0
    local recentlyMoved = false
    local Xdiff,Ydiff
    local forceFadeBack = false

    function toggleCockpitView()
        if (not isEnabled) then
            isEnabled = true
            addEventHandler ("onClientPreRender", root, updateCamera)
            addEventHandler ("onClientCursorMove",root, freecamMouse)
        else --reset view
            isEnabled = false
            setCameraTarget (localPlayer, localPlayer)
            removeEventHandler ("onClientPreRender", root, updateCamera)
            removeEventHandler ("onClientCursorMove", root, freecamMouse)
        end
    end

    function updateCamera ()
        if (isEnabled) then
    	
    	    local nowTick = getTickCount()
    		
    		-- check if the last mouse movement was more than idleTime ms ago
    		if (wasInVehicle and recentlyMoved and not fadeBack and startTick and nowTick - startTick > idleTime) or forceFadeBack then
    		    recentlyMoved = false
    		    fadeBack = true
    			if rotX > 0 then
    			    Xdiff = rotX / fadeBackFrames
    			elseif rotX < 0 then
    			    Xdiff = rotX / -fadeBackFrames
    			end
    			if rotY > 0 then
    			    Ydiff = rotY / fadeBackFrames
    			elseif rotY < 0 then
    			    Ydiff = rotY / -fadeBackFrames
    			end
    		end
    		
    		if fadeBack then
    	    
    	        executeCounter = executeCounter + 1
    		
    	        if rotX > 0 then
    		        rotX = rotX - Xdiff
    		    elseif rotX < 0 then
    		        rotX = rotX + Xdiff
    		    end
    		
    		    if rotY > 0 then
    		        rotY = rotY - Ydiff
    		    elseif rotY < 0 then
    		        rotY = rotY + Ydiff
    		    end
    		
    		    if executeCounter >= fadeBackFrames then
    		        fadeBack = false
    				executeCounter = 0
    		    end
    		
    		end
    		
            local camPosXr, camPosYr, camPosZr = getPedBonePosition (localPlayer, 6)
            local camPosXl, camPosYl, camPosZl = getPedBonePosition (localPlayer, 7)
            local camPosX, camPosY, camPosZ = (camPosXr + camPosXl) / 2, (camPosYr + camPosYl) / 2, (camPosZr + camPosZl) / 2
            local roll = 0
            
            inVehicle = isPedInVehicle(localPlayer)
            
    		-- note the vehicle rotation
            if not inVehicle then
                local rx, ry, rz = getElementRotation(localPlayer)
                
                if wasInVehicle then
                    rotX = rotX - math.rad(rz) --prevent camera from rotating when exiting a vehicle
                end
                cameraAngleX = rotX
                cameraAngleY = rotY
            end
            
            wasInVehicle = inVehicle
    		
            local freeModeAngleZ = math.sin(cameraAngleY)
            local freeModeAngleY = math.cos(cameraAngleY) * math.cos(cameraAngleX)
            local freeModeAngleX = math.cos(cameraAngleY) * math.sin(cameraAngleX)

            -- calculate a target based on the current position and an offset based on the angle
            local camTargetX = camPosX + freeModeAngleX * 100
            local camTargetY = camPosY + freeModeAngleY * 100
            local camTargetZ = camPosZ + freeModeAngleZ * 100

            -- Work out the distance between the target and the camera (should be 100 units)
            local camAngleX = camPosX - camTargetX
            local camAngleY = camPosY - camTargetY
            local camAngleZ = 0 -- we ignore this otherwise our vertical angle affects how fast you can strafe

            -- Calulcate the length of the vector
            local angleLength = math.sqrt(camAngleX*camAngleX+camAngleY*camAngleY+camAngleZ*camAngleZ)

            -- Normalize the vector, ignoring the Z axis, as the camera is stuck to the XY plane (it can't roll)
            local camNormalizedAngleX = camAngleX / angleLength
            local camNormalizedAngleY = camAngleY / angleLength
            local camNormalizedAngleZ = 0

            -- We use this as our rotation vector
            local normalAngleX = 0
            local normalAngleY = 0
            local normalAngleZ = 1

            local normalX = (camNormalizedAngleY * normalAngleZ - camNormalizedAngleZ * normalAngleY)
            local normalY = (camNormalizedAngleZ * normalAngleX - camNormalizedAngleX * normalAngleZ)
            local normalZ = (camNormalizedAngleX * normalAngleY - camNormalizedAngleY * normalAngleX)
            camTargetX = camPosX + freeModeAngleX * 100
            camTargetY = camPosY + freeModeAngleY * 100
            camTargetZ = camPosZ + freeModeAngleZ * 100
    		
            setCameraMatrix (camPosX, camPosY, camPosZ+0.2, camTargetX-140, camTargetY, camTargetZ, roll)
        end
    end


    function freecamMouse (cX,cY,aX,aY)
    	

    	if isCursorShowing() or isMTAWindowActive() then
    		mouseFrameDelay = 5
    		return
    	elseif mouseFrameDelay > 0 then
    		mouseFrameDelay = mouseFrameDelay - 1
    		return
    	end
    	
    	startTick = getTickCount()
    	recentlyMoved = true
    	if fadeBack then
    	    fadeBack = false
    		executeCounter = 0
    	end
        local width, height = guiGetScreenSize()
        aX = aX - width / 2 
        aY = aY - height / 2
    	
        rotX = rotX + aX * mouseSensitivity * 0.01745
        rotY = rotY - aY * mouseSensitivity * 0.01745

        local pRotX, pRotY, pRotZ = getElementRotation (localPlayer)
        pRotZ = math.rad(pRotZ)
        
    	if rotX > PI then
    		rotX = rotX - 2 * PI
    	elseif rotX < -PI then
    		rotX = rotX + 2 * PI
    	end
    	
    	if rotY > PI then
    		rotY = rotY - 2 * PI
    	elseif rotY < -PI then
    		rotY = rotY + 2 * PI
    	end
        if isPedInVehicle(localPlayer) then
            if rotY < -PI / 4 then
                rotY = -PI / 4
            elseif rotY > PI/15 then
                rotY = PI/15
            end
        else
            if rotY < -PI / 4 then
                rotY = -PI / 4
            elseif rotY > PI / 2.1 then
                rotY = PI / 2.1
            end
        end
    end











    function fps()
    if inUse == true then
    toggleCockpitView()
    end
    end
    --bindKey ( "mouse3", "down", fps)

    function aim_in()
    	if inUse == true then
    	removeEventHandler('onClientRender', root, renderVehCross )
    	addEventHandler('onClientRender', root, renderVehCross2 )
    	else
        removeEventHandler('onClientRender', root, renderVehCross )
    	end
    end
    --bindKey ("mouse2", "down", aim_in)


    function aim_out()
    	if inUse == true then
    	removeEventHandler('onClientRender', root, renderVehCross2 )
    	addEventHandler('onClientRender', root, renderVehCross )
    	end
    end
    --bindKey ("mouse2", "up", aim_out)


    function renderVehCross2 ()
    	if inUse == true then
    		dxDrawImage (w/2-28, h/2-32,58,58,"files/crosshair.png",0,0,0)
    		setCameraFieldOfView("player",50)
    	end
    end



    function replaceskin()
    	txd = engineLoadTXD ( "files/m4.txd" )
    	engineImportTXD ( txd, 356)
    	dff = engineLoadDFF ( "files/m4.dff", 356)
    	engineReplaceModel ( dff, 356)
    end
    addEventHandler ( "onClientResourceStart", getResourceRootElement(getThisResource()), replaceskin)



    addCommandHandler( "on",
    function ()
        setDevelopmentMode ( true )
    end
    )

server side :

    tion = isElementWithinColShape ( source, col_s )		
    local detection2 = isElementWithinColShape ( source, col_s2 )	
    local detection3 = isElementWithinColShape ( source, col_s3 )	
    	
        if detection then
    		local weap = getElementData ( mg, "weapon_base" )
    		local weap2 = getElementData ( mg, "weapon" )		
    		local x,y,z = getElementRotation (weap)
    		setElementRotation ( weap, 0,0,rot+97)
    		
    		if rot < 40 then				
    		--outputChatBox ( "< 40" )
    		--triggerClientEvent("right", root)
    		end
    		if rot > 140 then	
    		--outputChatBox ( "> 180" )
    		--triggerClientEvent("left", root)
    		end
    		
    		local x,y,z,rx,ry,rz = getElementAttachedOffsets (weap2)
    		if -rot2_x < 8 then
    		setElementAttachedOffsets ( weap2, x, y, z, rx, -rot2_x+10, rz)				
    		end		
        elseif detection2 then
    		local weap = getElementData ( mg2, "weapon_base" )
    		local weap2 = getElementData ( mg2, "weapon" )		
    		local x,y,z = getElementRotation (weap)
    		setElementRotation ( weap, 0,0,rot+97)
    		
    		if rot < 40 then				
    		--outputChatBox ( "< 40" )
    		--triggerClientEvent("right", root)
    		end
    		if rot > 140 then	
    		--outputChatBox ( "> 180" )
    		--triggerClientEvent("left", root)
    		end
    		
    		local x,y,z,rx,ry,rz = getElementAttachedOffsets (weap2)
    		if -rot2_x < 8 then
    		setElementAttachedOffsets ( weap2, x, y, z, rx, -rot2_x+10, rz)				
    		end	
    		
        elseif detection3 then
    		local weap = getElementData ( mg3, "weapon_base" )
    		local weap2 = getElementData ( mg3, "weapon" )		
    		local x,y,z = getElementRotation (weap)
    		setElementRotation ( weap, 0,0,rot+97)
    		
    		if rot < 40 then				
    		--outputChatBox ( "< 40" )
    		--triggerClientEvent("right", root)
    		end
    		if rot > 140 then	
    		--outputChatBox ( "> 180" )
    		--triggerClientEvent("left", root)
    		end
    		
    		local x,y,z,rx,ry,rz = getElementAttachedOffsets (weap2)
    		if -rot2_x < 8 then
    		setElementAttachedOffsets ( weap2, x, y, z, rx, -rot2_x+10, rz)				
    		end			
        end
    end
    addEvent("rotatePlayerWeapon", true)
    addEventHandler("rotatePlayerWeapon",getRootElement(), rotate)
     
     
     
     
    function fire_server(player,px,py,pz)
    local detection = isElementWithinColShape ( source, col_s )
    local detection2 = isElementWithinColShape ( source, col_s2 )
    local detection3 = isElementWithinColShape ( source, col_s3 )
    		
    if detection then
    triggerClientEvent("fire_client", getRootElement(), player,px,py,pz)
    elseif detection2 then
    triggerClientEvent("fire_client2", getRootElement(), player,px,py,pz)
    elseif detection3 then
    triggerClientEvent("fire_client3", getRootElement(), player,px,py,pz)
    end
    end
    addEvent("fire_server", true)
    addEventHandler("fire_server",getRootElement(), fire_server)
    function ready_server(player,x,y,z)
    triggerClientEvent("ready_client", getRootElement(), player,x,y,z)
    end
    addEvent("ready_server", true)
    addEventHandler("ready_server",getRootElement(), ready_server)

the script is createing a 2 or 3 machine gun's but i can't found the functoin of createing it to manipulte and mounted on a vehicle i want the machine gun to mount in the back of bobcat i think the code is flexible and my problem is esay to solve for those how has a exp in Lua thank alot

Link to comment

If you think anyone will help you if you just dump 959 lines of code, you're quite mistaken. The guides for this section clearly state that you're supposed to post relevant parts of the code, not the whole code. We're not here to make changes in a script you post here, we're here to help you make those changes.

14 hours ago, HustraDev said:

i can't found the functoin of createing it to manipulte and mounted on a vehicle

The function which creates the weapons is create_client and it is the first function in the client-side code. The code appears to take in arguments (weapon, weapon2, weapon3) which are all elements (presumably dummy elements, but I cannot tell since you didn't post the part of the code that involves triggering createRealWeapon). It is invoked by an event handler for createRealWeapon, so what you need is the server to createElement 3 weapon dummies (or whatever elements they're supposed to be), attach them to the vehicle where you want them attached, and triggerClientEvent createRealWeapon  passing along those elements.

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