Why is this not working correctly? I’m creating a raycast for my projectile, but it turns out it’s not detecting the parts correctly sometimes it, there are times it detects, but there are times it goes straight through
Maybe try making the multiplier higher, like *4 or *5 instead of *2. And I’d suggest converting your code to the new version of RayCasting (yes I noticed that you said that even the new RayCast doesn’t work properly). Something else you could do is to make an attachment in front of your sphere and get the LookVector from there so it’s more precise.
while game:GetService("RunService").Heartbeat:Wait() do
local ray = Ray.new(Beam.Position, Beam.Attachment.CFrame.lookVector)
local part, position = game.Workspace:FindPartOnRayWithIgnoreList(ray, {c , game.Workspace.Game.Explosion, game.Workspace.Game.Projectile})
if part ~= nil then
Beam:Destroy()
local Explosion = RP:WaitForChild("Part"):Clone()
Explosion.Parent = game.Workspace.Game.Explosion
Explosion.CFrame = CFrame.new(position)
local info = TweenInfo.new(2, Enum.EasingStyle.Back, Enum.EasingDirection.Out)
local tween = TweenService:Create(Explosion, info, {Size = Vector3.new(40,40,40), Transparency = 1})
tween:Play()
tween.Completed:Connect(function()
Explosion:Destroy()
end)
break
end
end
Just curious, is there a reason why you have all of this wrapped in a while loop? From the gif you provided it seems like you can run this check whenever the player clicks
It doesn’t need to be constantly updated, and is possibly the reason why this problem occurs.
local ray = Ray.new(Beam.Position, Beam.Attachment.CFrame.lookVector)
local part, position = game.Workspace:FindPartOnRayWithIgnoreList(ray, {c , game.Workspace.Game.Explosion, game.Workspace.Game.Projectile})
if part ~= nil then
Beam:Destroy()
local Explosion = RP:WaitForChild("Part"):Clone()
Explosion.Parent = game.Workspace.Game.Explosion
Explosion.CFrame = CFrame.new(position)
local info = TweenInfo.new(2, Enum.EasingStyle.Back, Enum.EasingDirection.Out)
local tween = TweenService:Create(Explosion, info, {Size = Vector3.new(40,40,40), Transparency = 1})
tween:Play()
tween.Completed:Connect(function()
Explosion:Destroy()
end)
try running all of that code when the player clicks:
--LocalScript
local player = game.Players.LocalPlayer
local mouse = player:GetMouse()
mouse.Button1Down:Connect(function()
-- Anything inside of here will run when a player clicks
end)