As you can see in the video, the player will move around and point their locked mouse towards an object. It is very inconsistent, which is caused by it only being detected at certain angles. The hitbox I made should be equally big on all sides. My question is how would I fix this so it works at all angles?
Script is below video.
local player = game.Players.LocalPlayer
local char = player.Character or player.CharacterAdded:Wait()
local cam = workspace.CurrentCamera
local uis = game:GetService("UserInputService")
local rs = game:GetService("RunService")
local remoteEvent = game:GetService("ReplicatedStorage").remoteEvents.destroyer
local params = RaycastParams.new()
params.FilterType = Enum.RaycastFilterType.Exclude
params.FilterDescendantsInstances = char:GetDescendants()
params.FilterDescendantsInstances = game.Workspace.id1Spawns:GetDescendants()
local function CreateRayCast()
local mousePos = uis:GetMouseLocation()
local rayCast = cam:ViewportPointToRay(mousePos.X,mousePos.Y)
local rcResult = workspace:Raycast(rayCast.Origin,rayCast.Direction * 20,params)
return rcResult
end
rs.RenderStepped:Connect(function()
local result = CreateRayCast()
if result and result.Instance then
local item = result.Instance:FindFirstAncestorWhichIsA("Model")
if item.Parent == game.Workspace.items then
print(item)
script.Parent.Parent.PlayerGui.CollectGui.Enabled = true
--remoteEvent:FireServer(item)
else
script.Parent.Parent.PlayerGui.CollectGui.Enabled = false
end
else
script.Parent.Parent.PlayerGui.CollectGui.Enabled = false
end
end)