local ReplicatedStorage = game:GetService("ReplicatedStorage")
local UIS = game:GetService("UserInputService")
local Root = script.Parent:WaitForChild("HumanoidRootPart")
UIS.InputBegan:Connect(function(input, gameProcessedEvent)
if gameProcessedEvent then return end
if input.UserInputType == Enum.UserInputType.MouseButton1 then
local unitRay = workspace.CurrentCamera:ViewportPointToRay(input.Position.X, input.Position.Y)
local origin = Root.Position
local direction = unitRay.Direction * 3
local raycastParams = RaycastParams.new()
raycastParams.FilterType = Enum.RaycastFilterType.Blacklist
raycastParams.FilterDescendantsInstances = {script.Parent}
local p2 = ReplicatedStorage.Part:Clone()
p2.Parent = workspace.Ignore
p2.Size = Vector3.new(0.2,0.2,1)
p2.Color = Color3.fromRGB(205, 180, 0)
for _ = 1, 100 do
local result = workspace:Raycast(origin, direction, raycastParams)
if result then
if result.Instance.Parent ~= workspace.Ignore then
p2.Position = result.Position
break
end
end
p2.CFrame = CFrame.lookAt(origin + direction, origin) * CFrame.new(0, 0, -direction.Magnitude / 2)
origin += direction
direction += Vector3.new(0, -0.01, 0)
task.wait()
end
task.delay(0.5, function()
p2:Destroy()
end)
end
end)
Produces these results: https://gyazo.com/225e2d52848bb4a4a77cb0e0b51f9804
Why is there such an offset when presumably ViewportPointToRay ignores any GUI offset?
P.S. I don’t want to use the Mouse
object if possible.