So I have a gun system in making, the gun fires the bullet, the bullet hits the wall, goes through, but instead of the otherside of the wall, it goes several studs ahead
code:
local ray, object, position
local muzzle = tool:WaitForChild("Muzzle")
local startPosition = muzzle.Position
local direction = muzzle.CFrame.LookVector
-- Creating the bullet
local bullet = Instance.new("Part")
bullet.Name = "Bullet_".. tostring(backendBulletCount)
bullet.FormFactor = Enum.FormFactor.Custom
bullet.Anchored = true
bullet.CanCollide = false
bullet.Massless = true
bullet.Locked = true
bullet.Parent = game.Workspace.Bullets -- Or anything you want to like in a folder in workspace
bullet.Material = Enum.Material.Neon
local light = Instance.new("PointLight")
light.Parent = bullet
light.Enabled = true
light.Brightness = 100
local hits = Instance.new("NumberValue")
hits.Name = "Hits"
hits.Parent = bullet
hits.Value = 0
soundModule.fire()
-- Ray casting function
local function RayCast()
--Debug(bullet)
ray = Ray.new(bullet.CFrame.p, bullet.CFrame.LookVector * bulletVelocity)
object, position = game.Workspace:FindPartOnRayWithIgnoreList(ray, {tool.Parent}, false, true)
local dropDamageModifier = (position - startPosition).Magnitude/bulletDropDistance
local _damage = damage/dropDamageModifier
if tonumber(_damage) > tonumber(damage) then
_damage = tonumber(damage)
end
if _damage < 1 then
_damage = 0
game:GetService("Debris"):AddItem(bullet, 0.01)
bullet = nil -- To remove the bullet object
end
if object and bullet then
bullet.Position = Vector3.new(bullet.Position.X + .2, bullet.Position.Y + .2, bullet.Position.Z + .2)
if object.Name ~= "DebugPart" then
hits.Value += 1
local humanoid = object.Parent:FindFirstChildWhichIsA("Humanoid")
if humanoid then
humanoid:TakeDamage(_damage)
--ragdollModule.ragdoll(object.Parent)
game:GetService("Debris"):AddItem(bullet, 0.01)
bullet = nil -- To remove the bullet object
else
bulletVelocity -= 25
if table.find(piercableMats, object.Material) then
print("Piercable! -- ".. tostring(object))
else
print("Not Piercable! -- ".. tostring(object))
end
end
end
end
end
bullet.Color = Color3.fromRGB(255, 150, 0)
bullet.Material = Enum.Material.Metal
bullet.Transparency = 0
bullet.Size = Vector3.new(0.05, 0.05, 0.7)
bullet.CFrame = CFrame.new(startPosition, startPosition + direction) * CFrame.new(0, 0, -0.7)
RayCast() -- Initial checking
local createBulletTrajectory = coroutine.wrap(function()
while bullet do
bullet.CFrame = bullet.CFrame * CFrame.new(0, 0, -bulletVelocity) * CFrame.Angles(-bulletDropAngle, 0, 0)
RayCast() -- Hit detection
end
end)
createBulletTrajectory() -- Running the coroutine thread
Example of whats happening:
Bullet went through the first wall, didint detect the next 2, or the first 2 dummies, went to the 3rd