Nextbot NPCs: trouble with attaching guns/weapons and fire direction
4 replies, posted
[U]I'm trying to code a couple of methods for adding gun support to NPCs. [/U]
[B]
[I]code:[/I]
[lua]
if SERVER then
function entity:IsNPC()
local name = self:GetClass()
if string.lower( string.sub(name, 1, 4) ) == "npc_" then
return true
else
return false
end
end
function giveNPCWeapon(ent)
if !IsValid(ent) || !ent:IsNPC() then return end
local att = "anim_attachment_RH"
local shootpos = ent:GetAttachment(ent:LookupAttachment(att))
local wep = ents.Create("weapon_bane_pistol")
wep:SetOwner(ent)
wep:SetPos(shootpos.Pos)
--wep:SetAngles(ang)
wep:Spawn()
wep:SetSolid(SOLID_NONE)
wep:SetParent(ent)
wep:Fire("setparentattachment", "anim_attachment_RH")
wep:AddEffects(EF_BONEMERGE)
wep:SetAngles(ent:GetForward():Angle())
ent.Weapon = wep
end
function fireNPCWeapon(ent)
if !ent.Weapon then return end
local wep = ent.Weapon
local muzzle = wep:GetAttachment(wep:LookupAttachment("muzzle"))
local spread = .1
local shootPos = muzzle.Pos
local bullet = {}
bullet.Num = 1
bullet.Src = shootPos
bullet.Dir = ent:GetForward() -- ENT:GetAimVector() equivalent
bullet.Spread = Vector( spread * 0.1 , spread * 0.1, 0)
bullet.Tracer = 1
bullet.TracerName = "Tracer"
bullet.Force = 50
bullet.Damage = 18
bullet.AmmoType = "Pistol"
wep:FireBullets(bullet)
--print("Fired bullets")
end
end
[/lua]
[B]
Problems:[/B]
[B]Resolved:[/B] - I need a better method for setting the Angle/Position of the gun; Sometimes it spawns out of place augmented with a strange angle.
[B]Resolved:[/B] - I need to know how to find the Vector for the direction the muzzle is facing for the Bullet table. I tried wep:GetForward() but that didn't seem to work.
Bump.
For the gun position, you should bonemerge the weapon to the hand.
For the direction, try using NPC:GetForward().
Thank you for the response I will try it.
[editline]22nd April 2013[/editline]
It works! Thank you so much.
[IMG]http://i38.tinypic.com/aaji9h.jpg[/IMG]
Here's the code for everyone:
[lua]
if SERVER then
function entity:IsNPC()
local name = self:GetClass()
if string.lower( string.sub(name, 1, 4) ) == "npc_" then
return true
else
return false
end
end
function giveNPCWeapon(ent)
if !IsValid(ent) || !ent:IsNPC() then return end
local att = "anim_attachment_RH"
local shootpos = ent:GetAttachment(ent:LookupAttachment(att))
local wep = ents.Create("weapon_bane_pistol") --change this for whatever weapon you want
wep:SetOwner(ent)
wep:SetPos(shootpos.Pos)
--wep:SetAngles(ang)
wep:Spawn()
wep:SetSolid(SOLID_NONE)
wep:SetParent(ent)
wep:Fire("setparentattachment", "anim_attachment_RH")
wep:AddEffects(EF_BONEMERGE)
wep:SetAngles(ent:GetForward():Angle())
ent.Weapon = wep
end
end
[/lua]
[editline]22nd April 2013[/editline]
Just one more thing, does anybody know how to find the direction for the Bullet table, relative to the muzzle location?
Ok, resolved the bullet issue too with npc:GetForward(). Thanks again robotboy. I'll update the OP so people can see the fixed code.
Sorry, you need to Log In to post a reply to this thread.