Skip to main content

Weapon Flashlight

This sample code attaches a Light to a Weapon to make a Flashlight attachment.

Insert this code in Server/Index.lua file:

Server/Index.lua
-- Spawns a Weapon
local new_weapon = Weapon(Vector(600, 0, 250), Rotator(0, 0, 0), "helix::SK_AK47")

---setup weapon
-- Configures how the Character Grabs and Aims the Weapon
new_weapon:SetHandlingMode(HandlingMode.DoubleHandedWeapon)
new_weapon:SetSightTransform(Vector(0, 0, -1), Rotator(-1.5, 0, 0))
new_weapon:SetLeftHandTransform(Vector(22, 0, 9), Rotator(0, 60, 90))
new_weapon:SetRightHandOffset(Vector(-10, 0, 0))

-- Configures Weapon Particles
new_weapon:SetParticlesBulletTrail("helix::P_Bullet_Trail")
new_weapon:SetParticlesBarrel("helix::P_Weapon_BarrelSmoke")
new_weapon:SetParticlesShells("helix::P_Weapon_Shells_762x39")

-- Configures Weapon Sounds
new_weapon:SetSoundDry("helix::A_Rifle_Dry")
new_weapon:SetSoundLoad("helix::A_Rifle_Load")
new_weapon:SetSoundUnload("helix::A_Rifle_Unload")
new_weapon:SetSoundZooming("helix::A_AimZoom")
new_weapon:SetSoundAim("helix::A_Rattle")
new_weapon:SetSoundFire("helix::A_AK47_Shot")

-- Configures Weapon Animations
new_weapon:SetAnimationFire("helix::A_AK47_Fire")
new_weapon:SetAnimationCharacterFire("helix::AM_Mannequin_Sight_Fire")
new_weapon:SetAnimationReload("helix::AM_Mannequin_Reload_Rifle")

--Weapon Flashlight:
-- Spawns a Spot Light (with color Black, to be turned on only when someone picks up it)
my_light = Light(Vector(), Rotator(), Color(0, 0, 0), 1, 1000, 1000, 35)

-- Attaches the Light to the Weapon with offset X = 100 (at the weapon's front)
my_light:AttachTo(my_weapon)
my_light:SetRelativeLocation(Vector(100, 0, 0))

-- Stores the light on the Weapon
my_weapon:SetValue("Light", my_light)

-- Only when someone picks up this weapon, turns on the Flashlight (set it's color)
my_weapon:Subscribe("PickUp", function(weapon, character)
local light = my_weapon:GetValue("Light")
if (light ~= nil) then
light:SetColor(Color(0.73, 0.67, 0.42))
end
end)

-- When the weapon is dropped, turns off the Flashlight (set it's color to black)
my_weapon:Subscribe("Drop", function(weapon, character)
local light = my_weapon:GetValue("Light")
if (light ~= nil) then
light:SetColor(Color(0, 0, 0))
end
end)

To add a character, add this code from the initial setup page.