Jump to content

[Question]Firing Weapon


HustraDev

Recommended Posts

Hello, i created a weapon "minigun" using Create Weapon and i'm trying to make it fire like a weapon the Player have by that i mean ( it fires when mouse is clicked and stops when is not), i know that ( fireWeapon is the function that i should use ) so i tried to do it but i failed.

also i have another question ( i'm willing to make a crosshair for the weapon and i have no idea how to make the weapon fires to the crosshair point) so if there is any examples or functions to give that will be great 

 

what i have tried :-

Client Side:-


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

function ClientfireWeapon()
    setWeaponState ( Weapon, "firing" )
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)

ServerSide:-


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

 

 

  • Thanks 1
Link to comment
11 minutes ago, IIYAMA said:

You know what the local keyword does right??? If not, watch this video.

local Weapon = createWeapon("minigun",0,0,0)

 


Weapon = createWeapon("minigun",0,0,0)

 


Extra


function ClientfireWeapon()
	if isElement(Weapon) then
		setWeaponState ( Weapon, "firing" )
	end
end

 

 

thanks for your reply, what about the crosshair ? 

  • Thanks 1
Link to comment

update :- i guess i get the idea of the crosshair and how it works, but there is two problems first is the weapon is "killing the Player when aiming down" the second one is the weapon movement, what i'm missing here?

 


function ClientfireWeapon()
    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])
    setWeaponState ( Weapon, "firing" )
end
addEvent("FireWeapon", true)
addEventHandler("FireWeapon",getRootElement(), ClientfireWeapon)

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

 

  • Thanks 1
Link to comment
  • Moderators
1 hour ago, HustraDev said:

thanks for your reply, what about the crosshair ? 

Before I give you recommendations. You will have to make a choice.

-[x] left and right?

- [ ] up and down? Complexity (synchronisation) +1

- [ ] crosshair based on camera? Complexity + 2

- [ ] crosshair based on turret?

- [ ] crosshair left/right based on camera Complexity + 1 + crosshair up/down based on turret?

Edited by IIYAMA
  • Thanks 1
Link to comment
3 hours ago, IIYAMA said:

 [ ] crosshair left/right based on camera Complexity + 1 + crosshair up/down based on turret?

This is me OP HustraDev, faced some trouble login with my aother account, first thank you for your reply l, and can you walk me thro, making it - [ ] crosshair left/right based on camera Complexity + 1 + crosshair up/down based on turret?, and i'm very grateful for your help

Link to comment
  • Moderators
4 hours ago, Hustra2 said:

- [ ] crosshair left/right based on camera Complexity + 1

I have experimented with the first part. The rest is for you to do.


setElementAlpha(localPlayer, 0)

addEventHandler("onClientResourceStop", resourceRoot, 
function ()
	setElementAlpha(localPlayer, 255)
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, -- ignoreSomeObjectsForCamera
		false, -- shootThroughStuff
		localPlayer -- element ignoredElement
	)
	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
		dxDrawCircle ( cursorX, cursorY, 10)
		dxDrawCircle (screenX / 2, screenY / 2, 10)
	end
end)

 

 

Edited by IIYAMA
  • Thanks 2
Link to comment
8 hours ago, IIYAMA said:

I have experimented with the first part. The rest is for you to do.


setElementAlpha(localPlayer, 0)

addEventHandler("onClientResourceStop", resourceRoot, 
function ()
	setElementAlpha(localPlayer, 255)
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, -- ignoreSomeObjectsForCamera
		false, -- shootThroughStuff
		localPlayer -- element ignoredElement
	)
	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
		dxDrawCircle ( cursorX, cursorY, 10)
		dxDrawCircle (screenX / 2, screenY / 2, 10)
	end
end)

 

 

i'm grateful for you help mate, see what i have done:-


function aim()
	local camRot = getPedCameraRotation(localPlayer)
    setElementAttachedOffsets(current.object, 0, -1.5, -0.25, 0, 0, -camRot+90)
    setElementRotation(localPlayer,0, 0, -camRot)
    
setElementAlpha(localPlayer, 0)

addEventHandler("onClientResourceStop", resourceRoot, 
function ()
	setElementAlpha(localPlayer, 255)
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.5 -- < 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, -- ignoreSomeObjectsForCamera
		false, -- shootThroughStuff
		localPlayer -- element ignoredElement
    )
    setElementData ( localPlayer, "attachedToWeapon_aim", {hitX,hitY,hitZ} )

	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
		dxDrawCircle ( cursorX, cursorY, 10)
		dxDrawCircle (screenX / 2, screenY / 2, 10)
	end
end)
    renderVehCross2()
end

 

  • Like 1
  • Thanks 1
Link to comment
  • Moderators
2 hours ago, HustraDev said:

setElementData ( localPlayer, "attachedToWeapon_aim", {hitX,hitY,hitZ} )

Be careful with this, doing this every frame will kill the network of other players that are not inside of the local-network.

 

The following function can be used to reduce update. (every 300 milliseconds)

if checkPassiveTimer("attachedToWeapon_aim", true, 300) then
	setElementData ( localPlayer, "attachedToWeapon_aim", {hitX,hitY,hitZ} )
end

It is a useful function (by me), source code can be found here: https://wiki.multitheftauto.com/wiki/CheckPassiveTimer

 

  • Thanks 1
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...