I've created a Weapon template that is configured to spawn projectiles. The weapon is spawned at runtime. When attached to a player, the weapon works as expected. When attached to an NPC controlled by a script, the weapon no longer fires projectile. What am I missing?
local npc = script.parent
---@type Weapon
local weapon = npc:FindChildByType('Weapon')
while true do
Task.Wait(1)
npc:MoveTo(GetAlternatePosition(npc), 1, true)
Task.Wait(1)
if weapon then
weapon:Attack()
end
end
Solution: Increase "Pierce Count" or position Weapon muzzle outside of NPC collision mesh.
Figured out the issue had to do with piercing: Because the projectile was spawned inside the NPC (due to the weapon position when spawned at runtime) it was immediately colliding and disappearing.
To get to that point I initially spawned the projectile in code, and the figured out that I need to increase collision count.
# NOT RECOMMENDED, weapon:Attack() works just as well when weapon is properly positioned
local projectile = Projectile.Spawn(weapon.projectileTemplateId, weapon:GetWorldPosition(), -Vector3.RIGHT)
projectile.speed = weapon.projectileSpeed
projectile.lifeSpan = weapon.projectileLifeSpan
projectile.gravityScale = weapon.projectileGravity
projectile.capsuleLength = weapon.projectileLength
projectile.capsuleRadius = weapon.projectileRadius
projectile.drag = weapon.projectileDrag
projectile.bouncesRemaining = weapon.projectileBounceCount
projectile.piercesRemaining = weapon.projectilePierceCount +1