Jump to content

[Help]tactical vehicle


HustraDev

Recommended Posts

Posted (edited)

Hello Guys, i'm working on this new script its a Mounted machine gun and i faced some problems, so first problem is in the `getOut` function supposed to make the player free by that i mean (detach the player from the minigun and the turret object `a minigun` and back to normal) and the second problem is, in drawin a crosshair for the weapon as you can see in the line [75] of the code it supposed to `drawImage`. but it does not < maybe i'm mistaken in writing the `drawImage` function under those lines of `cursorX` statement, if so please enlighten me

and last but not least i want the `Weapon` to appear so i probably remove the `SetElementAlpha` to zero and replace the Weapon Model with a realistic weapon (i.e MG42) so, is this can be accomplish ? replacing the actual minigun weapon with another one? i tried to use 

function m42()
txd = engineLoadTXD("m4.txd", 31)
engineImportTXD(txd, 31)
dff = engineLoadDFF("m4.dff", 31)
engineReplaceModel(dff, 31)
end
--i guess the Weapon Model is 356 i tried with it but it didn't change the wepaon

but its the same and if could replace the weapon how can i avoid the firing Rotation problems. because i think the weapon firing Rotation is already buggy. i really need this to work, beacuse i want to prove to myself that i'm capable of learning.

code:- [ client side]


local current
w, h = guiGetScreenSize ()

addEvent("aim", true)
addEventHandler("aim", resourceRoot, function(vehicle, object)
	current = { vehicle=vehicle, object=object}
    addEventHandler("onClientRender", root, aim)
     
setElementAlpha(localPlayer, 0)

addEventHandler("onClientResourceStop", resourceRoot, 
function ()
    setElementAlpha(localPlayer, 255)
    setPedAnimation(localPlayer)
end)

addEventHandler("onClientPreRender", root, 
function () 
	--[[
		Creating the direction vector
	]]
	local cameraX, cameraY, cameraZ, lx, ly, lz = getCameraMatrix()
	
	local directionVector = (Vector3(lx, ly, lz) - Vector3( cameraX, cameraY, cameraZ))
	directionVector:normalize ( )
	
	
	directionVector = directionVector * 300
	
	
	local screenX, screenY = guiGetScreenSize()

	
	local startPositionX, startPositionY, startPositionZ = x, y, z
	
		
	local turrentX, turretY, turretZ = getElementPosition(localPlayer) -- must be the turret position
	turretZ = turretZ - 0.2 -- < just for test purposes
	
	-- draw turret
	dxDrawLine3D(turrentX, turretY, turretZ, turrentX, turretY, turretZ - 0.5, tocolor(0,0,0))
	dxDrawLine3D(turrentX, turretY, turretZ - 0.5, turrentX + 0.5, turretY, turretZ - 1, tocolor(0,0,0))
	dxDrawLine3D(turrentX, turretY, turretZ - 0.5, turrentX - 0.5, turretY, turretZ - 1, tocolor(0,0,0))
	dxDrawLine3D(turrentX, turretY, turretZ - 0.5, turrentX, turretY - 0.5, turretZ - 1, tocolor(0,0,0))
	dxDrawLine3D(turrentX, turretY, turretZ - 0.5, turrentX, turretY + 0.5, turretZ - 1, tocolor(0,0,0))
	
	-- detect any hits
	local hit, hitX, hitY, hitZ, elementHit = processLineOfSight (
		turrentX, turretY, turretZ, 
		cameraX + directionVector.x, cameraY + directionVector.y, cameraZ + directionVector.z, 
		true, -- checkBuildings
		true,  -- checkVehicles
		true, -- checkPlayers
		true, -- checkObjects
		false, -- checkDummies
		false, -- seeThroughStuff
		false, -- shootThroughStuff
		localPlayer -- element ignoredElement
	)
	if checkPassiveTimer("attachedToWeapon_aim", true, 300) then
		setElementData ( localPlayer, "attachedToWeapon_aim", {hitX,hitY,hitZ} )
	end
	if not hit then
		hitX, hitY, hitZ  = cameraX + directionVector.x, cameraY + directionVector.y, cameraZ + directionVector.z
    end
	dxDrawText(hit and "COLLISION!" or "NO COLLISION!", 300, 300)
	
	
	-- draw the line + cursor
	dxDrawLine3D(turrentX, turretY, turretZ, hitX, hitY, hitZ)
	
	local cursorX, cursorY = getScreenFromWorldPosition (hitX, hitY, hitZ)
	if cursorX then
		dxDrawImage (w/2-28, h/2-32,58,58,"files/crosshair.png",0,0,0)
		dxDrawCircle (screenX / 2, screenY / 2, 10)
	end
end)
end)

function aim()
	local camRot = getPedCameraRotation(localPlayer)
    setElementAttachedOffsets(current.object, 0, -1.5, -0.25, 0, 0, -camRot+90)
    setElementRotation(localPlayer,0, 0, -camRot)
    local x,y,z = getWorldFromScreenPosition ( w/2, h/2, 50 )
    setElementData ( localPlayer, "attachedToWeapon_aim", {x,y,z} )
    local aim = getElementData ( localPlayer, "attachedToWeapon_aim" )
    setWeaponTarget(Weapon, aim[1],aim[2],aim[3])
    -- renderVehCross2()
end

function CreateRealWeapon(object)
    Weapon = createWeapon("minigun",0,0,0)
    setWeaponFiringRate(Weapon,70)
    setElementAlpha(Weapon,0)
    attachElements(Weapon,localPlayer,0.042,0.1,0.5,0.4,26,94)
    outputChatBox("Real Weapon Created At X:" .. x .. "Y:" .. y .. "Z:" .. z)
end
addEvent( "createWeapon", true )
addEventHandler( "createWeapon", localPlayer, CreateRealWeapon )

function renderVehCross2 ()
		setCameraFieldOfView("player",70)
end

function ClientfireWeapon()
    if isElement(Weapon) then
        setPedAnimation(localPlayer,'Attractors','Stepsit_loop')
        setWeaponProperty(Weapon, "fire_rotation", 0, -30, 0)
        setWeaponState ( Weapon, "firing" )
    end
end
addEvent("FireWeapon", true)
addEventHandler("FireWeapon",getRootElement(), ClientfireWeapon)

bindKey ("mouse1", "down", ClientfireWeapon)

function ClientReadyWeapon()
    setWeaponState(Weapon, "ready") --- stops the weapon when the key is up
end
bindKey ("mouse1", "up", ClientReadyWeapon)


function GetOut()
destroyElement(Weapon)
triggerServerEvent("remove",root)
    setElementAlpha(localPlayer, 255)
    setPedAnimation(localPlayer)
removeEventHandler("onClientRender",root,aim)
local x,y,z = getElementPosition(localPlayer)
setElementPosition(localPlayer,x,y+4,z)
end
bindKey ("O", "down", GetOut)

[server side] :-

addCommandHandler("createTactical", function(player)
x,y,z = getElementPosition(player)
createVehicle(getVehicleModelFromName("Bobcat"), x, y+3, z+3)
end)
addCommandHandler("aim", function(player)
    local vehicle = getPedOccupiedVehicle(player)
    if not vehicle then return outputChatBox("Use in vehicle...", player) end
	removePedFromVehicle(player)
    object = createObject(2985, 0, 0, 0)
    local x,y,z = getElementRotation(object)
    triggerClientEvent("createWeapon",player,object)
    setElementCollisionsEnabled(object, false)
	attachElements(object, vehicle, 0, -1.5, -0.25)
    attachElements(player, object,  -0.4, 0, 0.8)
    triggerClientEvent(player, "aim", resourceRoot, vehicle, object)
end)
addEvent("remove", true)
addEventHandler("remove",root,function(player)
    detachElements(object,player)
end)


function TriggerFire()
    local x,y,z = getElementPosition(localPlayer)
    triggerClientEvent("FireWeapon",getRootElement())
end

 

Thanks. 

 

Edited by HustraDev
  • Moderators
Posted
10 hours ago, HustraDev said:

but its the same and if could replace the weapon how can i avoid the firing Rotation problems.

You can attach an object to the weapon (same model) and set the opacity of the weapon-element to 0. This way you can change the attach offset in depended.

Do you want to improve your Lua programming skills and make less mistakes?   Start with Lua Language Server!   🙀

 

  Useful functions  3x 

  Tutorials  4x 

 

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