I'm having a problem where I repeatedly get the error "Unable to cast Vector3 to float" Whenever I run this script.
function TouchEnded(touch, gameProcessedEvent) RayPosition = touch.Position print("Touch ended at "..tostring(touch.Position)) end game:GetService("RunService").RenderStepped:Connect(function(dt) local Camera = workspace.CurrentCamera local rayDistance = 300 local viewportPoint = Camera.ViewportSize / 2 print(RayPosition) local unitRay = Camera:ViewportPointToRay(RayPosition, 0) local usableRay = Ray.new(unitRay.Origin, unitRay.Direction * rayDistance) part, hitPosition = workspace:FindPartOnRay(usableRay, char) if part then print("Hit part: " .. part:GetFullName()) end end)
The error appears on line 11. Any help will be appreciated
the function ViewportPointToRay doesn't accept vector3 values. It's arguments should be x,y,z
and not Vector3(x,y,z)
. To fix this, just convert the vector3 to x,y,z like this:
function TouchEnded(touch, gameProcessedEvent) RayPosition = touch.Position print("Touch ended at "..tostring(touch.Position)) end game:GetService("RunService").RenderStepped:Connect(function(dt) local Camera = workspace.CurrentCamera local rayDistance = 300 local viewportPoint = Camera.ViewportSize / 2 print(RayPosition) local unitRay = Camera:ViewportPointToRay(RayPosition.X, RayPosition.Y, 0) local usableRay = Ray.new(unitRay.Origin, unitRay.Direction * rayDistance) part, hitPosition = workspace:FindPartOnRay(usableRay, char) if part then print("Hit part: " .. part:GetFullName()) end end)
I'm just assuming you wanted the Z part to be 0