I am trying to achieve a realistic bullet and I’ve bumped into an issue where the Raycasting works normally but when I shoot the bullet straight up and falls down due to the bullet drop the Raycast does not work at all and the bullet falls through the ground
Code
local RunService = game:GetService("RunService")
local MathPi = math.pi
local CFrameNew = CFrame.new
local Vector3New = Vector3.new
local RayNew = Ray.new
local ColorSequenceNew = ColorSequence.new
local NumberSequenceNew = NumberSequence.new
local Color3FromRBG = Color3.fromRGB
local InstanceNew = Instance.new
local Tracers = {}
RunService.Heartbeat:Connect(function()
for i , TracerInfo in next, Tracers do
if not TracerInfo ~= nil and not ((tick() - TracerInfo.StartTick) > 5) then
local NextZ = -TracerInfo.Velocity / 10
local BulletDrop = (TracerInfo.Gravity / 2 * (tick() - TracerInfo.StartTick) ^ 2 / MathPi) * TracerInfo.Velocity
local NextFrame = TracerInfo.Attachment0.CFrame * CFrame.new(0 , 0 , NextZ) * CFrameNew(TracerInfo.Attachment0.CFrame:VectorToObjectSpace(Vector3New(0 , -BulletDrop , 0)))
TracerInfo.Attachment0.CFrame = NextFrame
TracerInfo.Attachment1.CFrame = TracerInfo.Attachment0.CFrame * CFrameNew(0 , 0 , -2.5)
local RayCast = RayNew(TracerInfo.Attachment0.CFrame.Position , TracerInfo.Attachment0.CFrame * Vector3New(0 , 0.5 , 0.5))
local Hit , _ , SurfaceNormal = workspace:FindPartOnRay(RayCast , script.Parent)
if Hit then
TracerInfo.Attachment0:Destroy()
TracerInfo.Attachment1:Destroy()
table.remove(Tracers , i)
end
else
TracerInfo.Attachment0:Destroy()
TracerInfo.Attachment1:Destroy()
table.remove(Tracers , i)
end
end
end)
local function Shoot()
local TracerInfo = {}
TracerInfo.Gravity = 0.25
TracerInfo.Velocity = 30
TracerInfo.Damage = 10
TracerInfo.StartTick = tick()
TracerInfo.Attachment0 = InstanceNew("Attachment")
TracerInfo.Attachment0.WorldCFrame = script.Parent.CFrame
TracerInfo.Attachment0.Parent = workspace.Terrain
TracerInfo.Attachment1 = InstanceNew("Attachment")
TracerInfo.Attachment1.WorldCFrame = script.Parent.CFrame
TracerInfo.Attachment1.Parent = workspace.Terrain
local Beam = InstanceNew("Beam")
Beam.Transparency = NumberSequenceNew(0)
Beam.Color = ColorSequenceNew(Color3FromRBG(255, 85, 0))
Beam.LightEmission = 1
Beam.LightInfluence = 0
Beam.Width0 = 0.25
Beam.Width1 = 0.25
Beam.FaceCamera = true
Beam.Attachment0 = TracerInfo.Attachment0
Beam.Attachment1 = TracerInfo.Attachment1
Beam.Parent = TracerInfo.Attachment0
table.insert(Tracers , TracerInfo)
end
while true do
Shoot()
wait(0.1)
end