automatic CIWS and Missile turret

If you liked this item, please rate it up on Steam Workshop page.

Author: Hijong park

Last revision: 22 May, 2022 at 01:56 UTC

File size: 1.73 MB

On Steam Workshop

Description:

Modified attack boat with CIWS and Missile turret.

CIWS automically attacks neraby aircrafts, but when the missile is detected it tries to intercept it (not always successful due to bullet spread)

Missile turret automically launch missiles to nearby aircrafts.

Consider this as a teaser of the Vanilla+ battleship mod Sofa is going to make.

behaviour("ciwsship")
function ciwsship:Start()
self.vehicle = self.gameObject.GetComponent(Vehicle)
self.tgt=nil
self.msltgt=nil
self.cooldown=0
end

function ciwsship:Update()
if self.vehicle.hasDriver then

self.msltgt=nil
local ac=ActorManager.AliveActorsInRange(self.targets.mslturret.transform.position,900)
if #ac>0 then
for i = 1, #ac do
if ac.team~=self.vehicle.driver.team and ActorManager.ActorCanSeePlayer(ac) and ac.activeVehicle~=nil and (ac.activeVehicle.isHelicopter or ac.activeVehicle.isAirplane) and (self.msltgt==nil or Vector3.distance(ac.transform.position,self.targets.mslturret.transform.position)<Vector3.distance(self.msltgt.transform.position,self.targets.mslturret.transform.position) ) then
self.msltgt=ac.activeVehicle
end
end
end

local mslrot=self.vehicle.transform.rotation

if self.msltgt~=nil then
mslrot=Quaternion.LookRotation(self.msltgt.transform.position-self.targets.mslturret.transform.position,self.vehicle.transform.up)

if Vector3.Angle(self.msltgt.transform.position – self.targets.mslturret.transform.position, self.targets.mslturret.transform.forward) <= 1 then
local wep = self.vehicle.seats[1].weapons[1]
wep.Unholster()
wep.Shoot(false)
end

end

self.targets.mslturret.transform.rotation=Quaternion.RotateTowards(self.targets.mslturret.transform.rotation, mslrot, 180 * Time.deltaTime)

local ismsl=false
self.tgt=nil
if self.vehicle.isTrackedByMissile then
local msl = self.vehicle.GetTrackingMissiles()
for i=1,#msl do
if self.tgt==nil or Vector3.distance(msl.transform.position,self.targets.turret.transform.position)<Vector3.distance(self.tgt.transform.position,self.targets.turret.transform.position) then
self.tgt=msl
ismsl=true
end
end
else
local ac=ActorManager.AliveActorsInRange(self.targets.turret.transform.position,500)
if #ac>0 then
for i = 1, #ac do
if ac.team~=self.vehicle.driver.team and ActorManager.ActorCanSeePlayer(ac) and ac.activeVehicle~=nil and (ac.activeVehicle.isHelicopter or ac.activeVehicle.isAirplane) and (self.tgt==nil or Vector3.distance(ac.transform.position,self.targets.turret.transform.position)<Vector3.distance(self.tgt.transform.position,self.targets.turret.transform.position) ) then
self.tgt=ac
end
end
end
end

local gyrorot=self.vehicle.transform.rotation

if self.tgt~=nil then

local tgtpos=self.tgt.transform.position+self.tgt.transform.up*0.65
local tgtspd=self.tgt.velocity
local bulletspd=350

local gyroguidepos=tgtpos
for i=1,5 do
gyroguidepos=tgtpos + (Vector3.distance(gyroguidepos,self.targets.muzzle.transform.position)/bulletspd)*( tgtspd )
end

if self.cooldown==0 and Vector3.Angle(gyroguidepos – self.targets.muzzle.transform.position, self.targets.muzzle.transform.forward) <= 1 then
local blt=self.gameObject.Instantiate(self.targets.bullet, self.targets.muzzle.transform.position,Quaternion.Euler(self.targets.muzzle.transform.eulerAngles.x-1+2*math.random(),self.targets.muzzle.transform.eulerAngles.y-1+2*math.random(),self.targets.muzzle.transform.eulerAngles.z))
if ismsl==true then
local bltsc=self.script.GetScript(blt.gameObject)
bltsc.tgtmsl=self.tgt
end

blt=blt.gameObject.GetComponent(Projectile)
blt.source=self.vehicle.driver
local snd=self.targets.muzzle.gameObject.GetComponent(AudioSource)
snd.play()
self.cooldown=0.05
end

gyrorot=Quaternion.LookRotation(gyroguidepos-self.targets.turret.transform.position,self.vehicle.transform.up)

if self.cooldown>0 then
self.cooldown=self.cooldown-Time.deltaTime
if self.cooldown<=0 then
self.cooldown=0
end
end

end

self.targets.turret.transform.rotation=Quaternion.RotateTowards(self.targets.turret.transform.rotation, gyrorot, 180 * Time.deltaTime)

end
end