Thanks smallg, this works great.
The only thing I would change if I knew how would be to delay the hurt or ricochet based on the player's distance. So if they are far away, there is a noticeable delay from gunshot to damage and if they are close it is near instant. Basically I'm trying to figure out whether to put...
if GetTimer(e) > (GetPlayerDistance(e)/8) then
...into the script. I guess the IntersectAll should be called at the moment of firing, then the 'outcomes' should be called later once ^that^ statement is true.
EDIT: OK, explain to me what I'm doing wrong here:
--sniper2 script by smallg
local fire_range = {}
local shot_delay = {}
local damage = {}
local chance_to_hit = {}
local fired = {}
function sniper3_init(e)
damage[e] = 30
fire_range[e] = 4000
shot_delay[e] = 6000 --1000 = 1 second
chance_to_hit[e] = 50 --100 = certain hit
CharacterControlManual(e)
CharacterControlArmed(e)
fired = 0
end
--sound1 = normal sniper gun shot
--sound2 = bullet hits flesh
--sound3 = bullet ricochets
function sniper3_main(e)
Prompt ("fired = "..fired..", time ="..GetTimer(e)..", Distance/8 = "..(GetPlayerDistance(e)/8))
if g_Entity[e]['animating'] == 0 then
SetAnimation(0)
PlayAnimation(e)
g_Entity[e]['animating'] = 1
end
if GetTimer(e) > shot_delay[e] then
StartTimer(e)
PlaySound(e,1)
if GetPlayerDistance(e) < fire_range[e] then
x1 = GetEntityPositionX(e)
y1 = GetEntityPositionY(e)+80 --above origin so we can see over cover
z1 = GetEntityPositionZ(e)
y2 = g_PlayerPosY
x2 = g_PlayerPosX
z2 = g_PlayerPosZ
obstructionhit = IntersectAll(x2,y2,z2,x1,y1,z1,g_Entity[e]['obj'])
fired = 1
if fired == 1 and GetTimer(e) > (GetPlayerDistance(e)/8) then
fired = 2
if fired == 2 then
if obstructionhit == nil then
obstructionhit = 0
PlayNon3DSound(e,3) --bullet ricochets
fired = 0
end
if obstructionhit ~= 0 then
fired = 0
else
if math.random(1,100) >= chance_to_hit[e] then
PlayNon3DSound(e,2) --bullet hits flesh
HurtPlayer(e,damage[e])
else
PlayNon3DSound(e,3) --bullet ricochets
end
end
end
end
end
end
end
AE