Using raycasting every frame on the server, I'm aware this completely ignores the size of the bullet. If I wanted to incorporate that would it be wise to just unanchor the bullet and use velocity with a touch listener? Or I could incorporate EgoMoose's Region3 module. Any ideas?
---@param origin CFrame
---@param whitelist table<int, Instance>
function Class:Launch(origin, whitelist)
local start = tick()
local raycastParams = RaycastParams.new()
raycastParams.FilterDescendantsInstances = whitelist or {}
raycastParams.FilterType = Enum.RaycastFilterType.Blacklist
raycastParams.IgnoreWater = true
local result = nil
local yVelocity = 0
while tick() < start + self.Lifetime and not result do
local step = RunService.Stepped:Wait()
local gravity = (workspace.Gravity / 196.2) * 9.807
local drop = (gravity / 2) * math.pow(step, 2) --drop = gravity/2 * t²
yVelocity = (yVelocity + drop)
local target = (origin * CFrame.new(0, 0, -self.Speed * step)) - Vector3.new(0, yVelocity, 0)
local rayResult = workspace:Raycast(origin.Position, (target.Position - origin.Position).Unit * (self.Speed * step), raycastParams)
if rayResult then
self.Projectile.CFrame = CFrame.new(rayResult.Position, target.Position)
if rayResult.Instance then
result = rayResult
end
else
self.Projectile.CFrame = CFrame.new(target.Position, origin.Position) * CFrame.Angles(0, math.rad(180), 0)
end
origin = target
end
print("Projectile took", tick() -start, "seconds")
if result then
print(result)
local surface = self:GetSurface(result)
print(surface)
end
end

1 comment
I don't think the touched event fires when the projectile is anchored, also a BodyPosition is not good for this at all