I’m very new to raycasting, so sorry if this is a simple answer but I can’t seem to find anything online about it. I’ve created a tool that fires multiple raycasts and when they hit a part they clone another part with a decal on it from replicated storage. The problem is that the rays only seem to hit one specific side of the parts, although rarely they will hit the backside (see below).
local laser = script.Parent
local rs = game:GetService("RunService")
local uis = game:GetService("UserInputService")
local radius = 0.1
local testpart = game:GetService("ReplicatedStorage"):WaitForChild("Testpart")
local Players = game:GetService("Players")
local player = Players.LocalPlayer
local character = player.Character
if not character or not character.Parent then
character = player.CharacterAdded:wait()
end
local head = character:WaitForChild("Head")
player.CameraMode = Enum.CameraMode.LockFirstPerson
local function makerayvisible(ray)
local midpoint = ray.Origin + ray.Direction/2
local part = Instance.new("Part")
part.Parent = game.Workspace
part.Anchored = true
part.CFrame = CFrame.new(midpoint, ray.Origin)
part.Size = Vector3.new(radius,radius,ray.Direction.magnitude)
part.Material = Enum.Material.Neon
part.Color = Color3.fromRGB(255, 0, 0)
part.CanCollide = false
part.CanQuery = false
part.Name = "Laser"
local Info = TweenInfo.new(1)
local properties = {Transparency = 1}
local Tween = game:GetService("TweenService"):Create(part,Info,properties)
Tween:Play()
end
laser.Equipped:Connect(function(Mouse)
Mouse.Button1Down:Connect(function()
local endpoint = Instance.new("Part")
endpoint.Parent = workspace
endpoint.Transparency = 1
endpoint.CanCollide = false
endpoint.Anchored = true
endpoint.CanQuery = false
while uis:IsMouseButtonPressed(Enum.UserInputType.MouseButton1) == true do
local thing1 = math.random(0,5)
local thing2 = math.random(0,5)
local thing3 = math.random(0,5)
local point = player:GetMouse().Hit.Position
endpoint.CFrame = CFrame.new(point + Vector3.new(thing1,thing2,thing3))
local startp = script.Parent.start.Position
local endp = endpoint.Position - startp
local ray = Ray.new(startp,endp)
makerayvisible(ray)
local hit, position, normal = game.Workspace:FindPartOnRay(ray)
if hit then
local testpart2 = testpart:Clone()
testpart2.Parent = workspace.Dots
testpart2.CFrame = CFrame.new(position, position + normal)
else
print("No hit")
end
rs.Heartbeat:Wait()
end
local descendants = game.Workspace:GetDescendants()
for _, descendant in pairs(descendants) do
if descendant.Name == "Laser" then
descendant:Destroy()
end
end
endpoint:Destroy()
end)
end)
Any help would be appreciated!