Bullet RayCasting

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
5 Likes

Your second argument to Ray.new() doesn’t make sense to me. What is the intent?

1 Like

@EmilyBendsSpace to Raycast in the direction the bullet is traveling on

@TheAviator01 updated with full code

1 Like

But you’re constructing a Ray whose length is roughly the distance of your Attachment to the center of its parent (presumably Terrain?) I really doubt you want that. The second argument should probably be some short vector in the direction of travel of the bullet, no? Normally it would be the bullet velocity times an approximate frame delta time.

@EmilyBendsSpace how would i get the bullet velocity then?

@TheAviator01 try pointing the shooter upwards and you will see that the beams will fall through the ground

To get any Part’s Velocity Speed, you can simply do print(Part.Velocity.Magnitude) to get the velocity. I think this is what you are looking for but I’m not certain.

No , its not a part its an attachment as you can see in the provided code

I tried setting the position after the raycast and yet the problem still occurs , the problem is the ray is traveling in front on the attachment no matter what so if the bullet is flipped upside down the ray would still fire on top so it will completely miss the ground and I don’t know how to cast the ray relative to the bullets velocity/ cast the ray in the direction the bullet is moving

Sorry to bump into this thread but I solved this issue months ago but forgot that I made this post