Here's my 2nd attempt for keeping an object some distance away from a turret barrel. I forgot in the 1st script to change angles[1] from degrees to radians before running sine and cosine.
I also needed a way for the turret to know which player is using it ("inuse" and "usingweapon" threads). Thankfully while the player is mounted, he can't trigger any trigger_multiples.
Run "thread turretgun", change the origin (this was tested on mohdm1), and change the model to a flak88 that can be usable.
The main problem with making a "bullet shield" trigger that temporarily makes the turret gunner have "nodamage": the very first bullet always hits the player before "nodamage" can run, same with explosions.
Try this out, the main issue is in the "shot" thread. If you comment out the "takedamage" line, you'll see that players always get hurt after the first triggering.
Code:
turretgun:
local.flak88 = spawn "statweapons/mg42_gun.tik"
local.flak88.origin = ( 725 -300 100 )
local.flak88.angles = ( 0 0 0 )
local.flak88 yawcenter 0
local.flak88 pitchcaps ( -135 135 0)
local.flak88 setplayerusable 1
local.flak88 setthread inuse // called when a player uses the turret
local.crate = spawn trigger_multiple spawnflags 128 // respond to bullets, explosions, projectiles, etc
local.crate model "static/indycrate.tik"
local.crate setsize ( -75 -75 -75 ) ( 75 75 75 ) // indycrate dimensions are 71 x 47.
local.crate.origin = local.flak88.origin
local.crate.angles = local.flak88.angles
local.crate show // triggers are notsolid and invisible by default
local.crate.flak88 = local.flak88
local.crate setthread shot
local.trigger = spawn trigger_multiple
local.trigger.origin = local.flak88.origin
local.trigger setsize ( -75 -75 -75 ) ( 75 75 75 )
local.trigger.flak88 = local.flak88
local.trigger setthread usingweapon
local.crate.flak88 = local.flak88
local.crate.originold = local.flak88.origin // keep memory of the flak88's initial origin.
local.crate thread movetrig
end
inuse:
local.player = parm.other
self.gunner = local.player // keep memory of who just triggered the turret ("gunner" is not originally from MOHAA, so I had to add one here).
waitframe
self.gunner.usingweapon = 1
self.inuse = 1
end
usingweapon:
self nottriggerable
local.player = parm.other
if(self.flak88 == NULL && (self.flak88.gunner == NIL || self.flak88.gunner == NULL)) { self remove; end } // if the turret is gone, delete this trigger.
if(self.flak88.inuse != 1 || self.flak88.gunner.usingweapon != 1) { self triggerable; end } // if no one is using the turret yet, end.
if(local.player == self.flak88.gunner) { self.flak88.gunner.usingweapon = 0; self.flak88.inuse = 0; self.flak88.gunner = NIL } // run this only after the player exits the turret
self triggerable
end
shot:
if(self.flak88.gunner != NIL)
{
self nottriggerable
self.flak88.gunner nodamage // this does not stop players from dying by explosions
waitframe
if(self.flak88.gunner != NIL) { self.flak88.gunner takedamage } // the first bullet always hits, before the gunner receives the "nodamage" command.
self triggerable
}
end
movetrig:
local.PI = 3.14159265359 // MOHAA console may only display first 3 decimals (3.142), but the game does compute beyond 3 decimals.
local.distance = 80 // how far away from the flak88's origin
while(self.flak88 != NULL)
{
if(self.flak88.gunner != NIL) { local.fwd_vec = angles_toforward self.flak88.gunner.viewangles } // fwd_vec[2] = 0 when player looks horizontal, -0.999 looking down, 0.999 looking up.
if(self.flak88.gunner == NIL) { local.fwd_vec = angles_toforward self.flak88.angles }
local.verticaldist = local.fwd_vec[2] * local.distance
local.horizontaldist = local.fwd_vec[2]
if(local.fwd_vec[2] < 0) { local.horizontaldist = local.fwd_vec[2] * -1 } // horizontaldist = 0.999 looking down, 0.999 looking up.
if(self.flak88.gunner != NIL) { local.degrees = self.flak88.gunner.viewangles[1]; self.angles = self.flak88.gunner.viewangles }
if(self.flak88.gunner == NIL) { local.degrees = self.flak88.angles[1]; self.angles = self.flak88.angles }
if(local.degrees >= 180) { local.degrees = local.degrees - 360 } // local.player.angles[1] = local.degrees can sometimes be > 180 and < 360.
local.radians = local.degrees * (local.PI / 180) // for sin/cos to work, degrees must be between -180, 0, 180.
local.sin = (sin local.radians) * (local.distance - (local.horizontaldist * local.distance))
local.cos = (cos local.radians) * (local.distance - (local.horizontaldist * local.distance))
self.origin = self.originold + ( local.cos local.sin local.verticaldist )
waitframe
}
local.fwd_vec = ( 0 0 0 )
self remove
end