I’m having problems with my camera drag system, for context the x and y values are as calculated:
(mouse X /viewport X - 0.5) * 2
same with the Y value
Left Raycast is a raycast from the camera’s CFrame to -fov/2
, and Right Raycast is a raycast from the camera’s CFrame to fov/2
.
The problem is with that the Raycast for whatever reason does not hit the surface even though inputs should be correct.
Here’s the source code:
mouse.Button1Down:Connect(function()
LeftHeld = true
local startingat = CameraPart.Position
local upCxt : RBXScriptConnection
local moveCnxt : RBXScriptConnection
local rParams = RaycastParams.new()
rParams.FilterType = Enum.RaycastFilterType.Whitelist
rParams.FilterDescendantsInstances = {workspace.Baseplate}
local mouseStartedAt = Vector2.new((mouse.X/camera.ViewportSize.X-.5)*2,(mouse.Y/camera.ViewportSize.Y-.5)*2)
local function OnMove()
local LeftRay = workspace:Raycast(camera.CFrame.Position,Vector3.new(camera.FieldOfView/2*-1,0,0))
local RightRay = workspace:Raycast(camera.CFrame.Position,Vector3.new(camera.FieldOfView/2,0,0))
local Range = 4
if LeftRay and RightRay then
Range = (LeftRay.Position-RightRay.Position).Magnitude
end
local TrueX = (mouse.X/camera.ViewportSize.X-.5)*2-mouseStartedAt.X
local TrueY = (mouse.Y/camera.ViewportSize.Y-.5)*2-mouseStartedAt.Y
CameraPart.Position =
startingat + Vector3.new(
TrueX*Range/2,
0,
TrueY*(Range/2)*(camera.ViewportSize.Y/camera.ViewportSize.X)
)
end
local function UpConnect()
moveCnxt:Disconnect()
upCxt:Disconnect()
end
upCxt = mouse.Button1Up:Connect(UpConnect)
moveCnxt = mouse.Move:Connect(OnMove)
end)
For context CameraPart
is a client-sided Part