Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

Unable to cast Vector3 to float?

Asked by
Memotag 226 Moderation Voter
5 years ago

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

0
the error is saying you can't enter a vector3 value to a float (I think) Jack_Hase 85 — 5y
0
That's as much as I know. I can't figure out how to fix it Memotag 226 — 5y
0
That's why I didn't post an answer Jack_Hase 85 — 5y

1 answer

Log in to vote
1
Answered by 5 years ago

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

0
Thank you. That's exactly what I was looking for. Memotag 226 — 5y
0
No problem :) Internal_1 344 — 5y
Ad

Answer this question