CIWS / CRAM Aim at air target (If you know math, it'll be easy). (Help urgent)

So basically, if your not a military expert, heres what a CIWS / CRAM is.
It tracks a moving objects position, calculates its speed, + its bullet speed to reach target, and fires on that position to intercept the target.
(Accuracy is important, but if it misses the bullets explode near the target to do damage anyway so yeah)

Video demonstration.

What im planning to do is, detect a special part named “Epicness” and target that part and aim at it accurately when its moving in the air, about bullet speed ill talk to you about that soon if i even get a reply

How would i recreate such a thing using hinges?

If anything, first step, lets start off with even aiming at the part, then we can talk about bullet speed etc. (I know how to do bullets, just not calculate their speed in a script like this).
image

2 Likes

To look at the part/projectile, you can use CFrame.new(originPosition, targetPosition)

To calculate the speed, you can check the projectile Velocity

How would i convert this into two hinges tho, like their target angle?
(HorizontalRotator is the base, moves it horizontally left and right) (Color: Red)
(VeritcalRotator is the gun) moves it up and down. (Color: Yellow)
image

image

1 Like

Afraid can’t help with that, haven’t worked with HingeConstraints, but I think there should be plenty tutorials on YouTube how to mess around with 'em.

Unsure what your hinge set up is like, but something like this is probably what you want:

-- const to convert radians to degrees
local RAD2DEG = 1 / (math.pi * 2 / 360)

-- the up vector of the turret
--   e.g. in cases where the turret isn't on flat ground etc
local upVector = Vector3.yAxis

-- the position to target
--   e.g. somePlayer.Character.Head.Position
local targetPosition = Vector3.new(0, 5, 5)

-- the point from which we'll aim
--   i.e. the centre point of the pitch hinge/axel on the turret
local centrePosition = part.Position

-- calculate the yaw / pitch
local lookAt = CFrame.lookAt(centrePosition, targetPosition, upVector)
local pitch, yaw, roll = lookAt:ToOrientation()

-- apply to hinge(s)
yawHinge.TargetAngle = yaw*RAD2DEG
pitchHinge.TargetAngle = pitch*RAD2DEG

Ok , did this do you know how to calculate the time to reach for a bullet with bodyforce? (i’ll provide its bodyforce code below)

local module = {}
local ABS, HUGE, FLOOR, CEIL = math.abs, math.huge, math.floor, math.ceil
local RAD, SIN, COS, TAN = math.rad, math.sin, math.cos, math.tan
local VEC2, V3 = Vector2.new, Vector3.new
local CF, CFANG = CFrame.new, CFrame.Angles
local INSERT = table.insert


local Walking = false

local instance = Instance.new
local CFn = CFrame.new
local CFa = CFrame.Angles
local asin = math.asin
local abs = math.abs
local min = math.min
local max = math.max
local random = math.random
local RS = game:GetService("RunService")

local BDrop = 0.1
local BSpeed = 800


function Lerp(n,g,t)
	return n+(g-n)*t
end

function RAND(Min, Max, Accuracy)
	local Inverse = 1 / (Accuracy or 1)
	return (math.random(Min * Inverse, Max * Inverse) / Inverse)
end
local TS = game:GetService("TweenService")


function module.CastRay(Bala,POS,char)
	task.spawn(function()
	local Hit2, Pos2, Norm2, Mat2
	local Hit, Pos, Norm, Mat
	local L_257_ = POS;
	local L_258_ = Bala.Position;
	local TotalDistTraveled = 0
	local recast
	local Ray_Ignore = {char}
	

		while true do
			RS.Heartbeat:wait()
			L_258_ = Bala.Position;
			TotalDistTraveled = TotalDistTraveled + (L_258_ - L_257_).magnitude

			Hit2, Pos2, Norm2, Mat2 = workspace:FindPartOnRayWithIgnoreList(Ray.new(L_257_, (L_258_ - L_257_)*20), Ray_Ignore, false, true);

			Hit, Pos, Norm, Mat = workspace:FindPartOnRayWithIgnoreList(Ray.new(L_257_, (L_258_ - L_257_)), Ray_Ignore, false, true);

			Bala.CFrame=CFrame.new(Bala.Position,Bala.Position+Bala.Velocity)*CFrame.Angles(math.pi/2,math.pi*BDrop,0)


			if Hit2 then
				while not recast do
					if Hit2 and (Hit2 and Hit2.Transparency >= 1 or Hit2.CanCollide == false and Hit2.Transparency == 1 or Hit2.Name == "Ignorable" or Hit2.Name == "Glass" or Hit2.Parent.Name == "Top" or Hit2.Parent.Name == "Helmet" or Hit2.Parent.Name == "Up" or Hit2.Parent.Name == "Down" or Hit2.Parent.Name == "Face" or Hit2.Parent.Name == "Olho" or Hit2.Parent.Name == "Headset" or Hit2.Parent.Name == "Numero" or Hit2.Parent.Name == "Vest" or Hit2.Parent.Name == "Chest" or Hit2.Parent.Name == "Waist" or Hit2.Parent.Name == "Back" or Hit2.Parent.Name == "Belt" or Hit2.Parent.Name == "Leg1" or Hit2.Parent.Name == "Leg2" or Hit2.Parent.Name == "Arm1"  or Hit2.Parent.Name == "Arm2") and Hit2.Name ~= 'Right Arm' and Hit2.Name ~= 'Left Arm' and Hit2.Name ~= 'Right Leg' and Hit2.Name ~= 'Left Leg' and Hit2.Name ~= 'Armor' and Hit2.Name ~= 'EShield' then
						table.insert(Ray_Ignore, Hit2)
						recast = true
					end

					if recast then
						Hit2, Pos2, Norm2, Mat2 = workspace:FindPartOnRayWithIgnoreList(Ray.new(L_257_, (L_258_ - L_257_)*20), Ray_Ignore, false, true);
						Hit, Pos, Norm, Mat = workspace:FindPartOnRayWithIgnoreList(Ray.new(L_257_, (L_258_ - L_257_)), Ray_Ignore, false, true);
						recast = false
					else

						break
					end
				end
			end
			local NewPart = Instance.new("Part")
			
			if Hit and not recast then

				Bala:Destroy()
				if Hit.Parent:FindFirstChild("Humanoid") then
					Hit.Parent:FindFirstChild("Humanoid"):TakeDamage(5)
				end

				if Hit.Parent.Parent:FindFirstChild("Humanoid") then
					Hit.Parent.Parent:FindFirstChild("Humanoid"):TakeDamage(5)
				end

				if Hit.Parent.Parent.Parent ~= workspace and Hit.Parent.Parent.Parent ~= nil then
				if Hit.Parent.Parent.Parent:FindFirstChild("Humanoid") then
					Hit.Parent.Parent.Parent:FindFirstChild("Humanoid"):TakeDamage(5)
				end
				end
				if Hit.Parent:FindFirstChild("Enemy") then
					Hit.Parent:FindFirstChild("Enemy"):TakeDamage(5)
				end

				if Hit.Parent.Parent:FindFirstChild("Enemy") then
					Hit.Parent.Parent:FindFirstChild("Enemy"):TakeDamage(5)
				end

				if Hit.Parent.Parent.Parent ~= workspace and Hit.Parent.Parent.Parent ~= nil then
					if Hit.Parent.Parent.Parent:FindFirstChild("Enemy") then
						Hit.Parent.Parent.Parent:FindFirstChild("Enemy"):TakeDamage(5)
					end
				end
				break
			end		
			L_257_ = L_258_;


		end
	end)
end

function module.CreateBullet(BSpread,CF,POS,bul)

	local Bullet = Instance.new("Part")
	Bullet.Name = "C_Bullet"
	Bullet.CanCollide = false
	Bullet.Transparency = 1
	Bullet.FormFactor = "Custom"
	Bullet.Size = Vector3.new(1,1,1)

	local BulletMass = Bullet:GetMass()
	local Force = Vector3.new(0,BulletMass * (game.Workspace.Gravity) - (BDrop) * (game.Workspace.Gravity), 0)

	local Origin = POS
	local Direction = CF.lookVector + (CF.upVector * (((BDrop*1/2.8)/BSpeed))/2)
	local BulletCF = CF
	local balaspread = CFrame.Angles(
		RAD(RAND(-BSpread - ((0/1)*0), BSpread + ((0/1)*0)) / 20),
		RAD(RAND(-BSpread - ((0/1)*0), BSpread + ((0/1)*0)) / 20),
		RAD(RAND(-BSpread - ((0/1)*0), BSpread + ((0/1)*0)) / 20)
	)
	Direction = balaspread * Direction	



	Bullet.CFrame = BulletCF + Direction
	
	local NewClone = bul:Clone()
	NewClone.CFrame = Bullet.CFrame
	NewClone.Parent = Bullet
	local NewWeld = Instance.new("WeldConstraint")
	NewWeld.Part0 = Bullet
	NewWeld.Part1 = NewClone
	NewWeld.Parent = Bullet

	Bullet.Parent = script.Parent.Parent
	Bullet.Velocity = Direction * BSpeed
	local BF = Instance.new("BodyForce")
	BF.force = Force
	BF.Parent = Bullet
	

	--CreateShell()

	game.Debris:AddItem(Bullet, 5)
	return Bullet

end
return module

Bspread is bullet spread, CF is the shooter part’s CFRAME, POS is the shooter parts Position,char is just the turret model.

Maybe we can calculate it with BSpeed?