Skip to main content

Cannon

Fires a cannon ball when triggered or clicked. Must be supplied with an ammo material. Iron is prioritized, and results in one large cannonball being shot. Copper results in grapeshot. It is a craftable and spawnable non-flammable solid.

Here is a list of possible sizes that reach the maximum malleability (80) that have integer components: 1x1x80, 1x2x40, 1x4x20, 1x5x16, 1x8x10, 2x2x20, 2x4x10, 2x5x8, 4x4x5

At its default size (2x8x2) it has a durability of 15, at its maximum size it has a durability of 21.

By default, its colour is #635f62.

It requires 4 Flint, 12 Iron, and 4 Sulfur to be crafted.

local PROJECTILE_GRAVITY = 100
local PROJECTILE_SPEED_PER_BARREL_STUD = {
Artillery = 75,
Cannon = 50,
}

-- Point a gyro in a direction such that the provided projectile launcher (often
-- an artillery or cannon) perfect hits the target.
local function pointAtTarget(
gyro: Gyro,
projectileLauncher: (Artillery | Cannon | PilotObject)?,
target: Vector3,
solution: ("direct" | "indirect")?
): Vector3?
local lookVector

local projectileSpeed = if projectileLauncher
then PROJECTILE_SPEED_PER_BARREL_STUD[projectileLauncher.ClassName]
else nil

if projectileSpeed then
local gyroCFrame = CFrame.lookAt(gyro.Position, Vector3.new(target.X, gyro.Position.Y, target.Z))
local relativeCFrame = gyroCFrame:PointToObjectSpace(target)

local dx, dy = -relativeCFrame.Z, relativeCFrame.Y
local velocity = projectileLauncher.Size.Y * projectileSpeed

local discriminant = velocity ^ 4 - PROJECTILE_GRAVITY * (PROJECTILE_GRAVITY * dx ^ 2 + 2 * dy * velocity ^ 2)
if discriminant < 0 then
return
end

local solutionSign = if solution == "indirect" then 1 else -1

local yDirection = velocity ^ 2 + math.sqrt(discriminant) * solutionSign
local xDirection = PROJECTILE_GRAVITY * dx
local pitch = math.atan2(yDirection, xDirection)

local lookCFrame = gyroCFrame * CFrame.Angles(pitch, 0, 0)
lookVector = lookCFrame.LookVector
else
lookVector = (target - gyro.Position).Unit
end

gyro:PointAlong(lookVector)

return lookVector
end