I’m trying to make a racyast for the bullets that come from a gun. The issue is that the raycast only works and damages humanoids if the bullet is inside a character model. There are no errors, what am I doing wrong?
local raycastOrigin = bullet.Position
local raycastDirection = bullet.CFrame.LookVector * 5
local rayParams = RaycastParams.new()
rayParams.FilterType = Enum.RaycastFilterType.Exclude
rayParams.FilterDescendantsInstances = {bullet, tool.Parent}
local raycastResult = workspace:Raycast(raycastOrigin, raycastDirection, rayParams)
while task.wait() do
if raycastResult ~= nil then
if raycastResult.Instance then
if raycastResult.Instance.Parent:FindFirstChildOfClass("Humanoid") then
local char = raycastResult.Instance.Parent
local humanoid = char:FindFirstChildOfClass("Humanoid")
print(raycastResult.Instance)
if raycastResult.Instance.Name == "Head" then
humanoid:TakeDamage(headShotDamage)
elseif raycastResult.Instance.Name == "Torso" or raycastResult.Instance.Name == char.PrimaryPart then
humanoid:TakeDamage(bodyShotDamage)
else
humanoid:TakeDamage(limbShotDamage)
end
break
end
end
end
end
raycastResult = nil
You need to increase the direction magnitude and also get rid of your loop.
Code:
local raycastOrigin = bullet.Position
local raycastDirection = bullet.CFrame.LookVector * 50
local rayParams = RaycastParams.new()
rayParams.FilterType = Enum.RaycastFilterType.Exclude
rayParams.FilterDescendantsInstances = {bullet, tool.Parent}
local raycastResult = workspace:Raycast(raycastOrigin, raycastDirection, rayParams)
if raycastResult then
if raycastResult.Instance.Parent:FindFirstChildOfClass("Humanoid") then
local char = raycastResult.Instance.Parent
local humanoid = char:FindFirstChildOfClass("Humanoid")
print(raycastResult.Instance)
if raycastResult.Instance.Name == "Head" then
humanoid:TakeDamage(headShotDamage)
elseif raycastResult.Instance.Name == "Torso" or raycastResult.Instance.Name == char.PrimaryPart then
humanoid:TakeDamage(bodyShotDamage)
else
humanoid:TakeDamage(limbShotDamage)
end
end
end
raycastResult = nil
Is there a way for me to visualize the raycast? For one, I still need to have the gun inside the character model, and for two even when multiplying the look vector by something like 50,000 it still doesn’t register to hit the character model at all, even when not only the raycast intercepts a part of the character model but also while the bullet is touching it.
I think doing so would help me ten fold in finding the issue.
It prints the body part hit, but the thing is this only applies to when the gun is inside of the body part, which is why I need to know how I can visualize the raycast to see what I am doing wrong.
local function visualizeRay(Start: Vector3, End: Vector3)
local Part = Instance.new("Part")
local MidPoint = (Start + End) / 2
local Length = (Start - End).Magnitude
Part.Anchored = true
Part.CanCollide = false
Part.CanTouch = false
Part.CanQuery = false
Part.Size = Vector3.new(0.1, 0.1, Length)
Part.CFrame = CFrame.new(MidPoint, End)
Part.Parent = workspace // so you can see it
return Part
end
// you can change the part however you like
// ex
// local part = visualizeRay({start}, {end})
// part.Color = Color3.new(1, 0, 0) (red)