I'm having a problem where I repeatedly get the error "Unable to cast Vector3 to float" Whenever I run this script.
01 | function TouchEnded(touch, gameProcessedEvent) |
02 | RayPosition = touch.Position |
03 | print ( "Touch ended at " .. tostring (touch.Position)) |
04 | end |
05 |
06 | game:GetService( "RunService" ).RenderStepped:Connect( function (dt) |
07 | local Camera = workspace.CurrentCamera |
08 | local rayDistance = 300 |
09 | local viewportPoint = Camera.ViewportSize / 2 |
10 | print (RayPosition) |
11 | local unitRay = Camera:ViewportPointToRay(RayPosition, 0 ) |
12 | local usableRay = Ray.new(unitRay.Origin, unitRay.Direction * rayDistance) |
13 |
14 | part, hitPosition = workspace:FindPartOnRay(usableRay, char) |
15 | if part then |
16 | print ( "Hit part: " .. part:GetFullName()) |
17 | end |
18 | 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:
01 | function TouchEnded(touch, gameProcessedEvent) |
02 | RayPosition = touch.Position |
03 | print ( "Touch ended at " .. tostring (touch.Position)) |
04 | end |
05 |
06 | game:GetService( "RunService" ).RenderStepped:Connect( function (dt) |
07 | local Camera = workspace.CurrentCamera |
08 | local rayDistance = 300 |
09 | local viewportPoint = Camera.ViewportSize / 2 |
10 | print (RayPosition) |
11 | local unitRay = Camera:ViewportPointToRay(RayPosition.X, RayPosition.Y, 0 ) |
12 | local usableRay = Ray.new(unitRay.Origin, unitRay.Direction * rayDistance) |
13 |
14 | part, hitPosition = workspace:FindPartOnRay(usableRay, char) |
15 | if part then |
16 | print ( "Hit part: " .. part:GetFullName()) |
17 | end |
18 | end ) |
I'm just assuming you wanted the Z part to be 0