so im trying to create a fruit ninja mini-game.
my problem is that my objects are spawning more so towards the center and right of the viewportframe rather than the whole way across. it’s disregarding the left side. i know it’s to do with how i’m incorrectly calculating it but i can’t find a solution so far. ![]()
thank u so much!
local rng = Random.new()
local CAMERA_DIST_OFFSET = 10 -- tweak to make the camera zoom in or out
local function calculateItemPlacementAndCamera(item: Model, ViewportCam: Camera)
-- centering / positioning part and viewport cam
local orientation, size = item:GetBoundingBox()
local rot = CFrame.Angles(0, math.rad(90), 0)
size = rot:VectorToObjectSpace(size)
local sizeX, sizeY, sizeZ = math.abs(size.X), math.abs(size.Y), math.abs(size.Z)
local fovRadians = math.rad(ViewportCam.FieldOfView / 2)
local h = (sizeY / (math.tan(fovRadians) * 2)) + (sizeZ / 2)
h = h + CAMERA_DIST_OFFSET
-- can include rot
local camCF = orientation * CFrame.new(0, 0, h)
-- random x positioning
local width, height = ViewportCam.ViewportSize.X, ViewportCam.ViewportSize.Y
-- calculating boundary along the horizontal center line of the cam's view
local leftRay = ViewportCam:ScreenPointToRay(0, height / 2)
local leftEdgePoint = leftRay.Origin + leftRay.Direction * h
local rightRay = ViewportCam:ScreenPointToRay(width, height / 2)
local rightEdgePoint = rightRay.Origin + rightRay.Direction * h
local dynamic_X_MIN = leftEdgePoint.X
local dynamic_X_MAX = rightEdgePoint.X
local randomPosX = rng:NextInteger(dynamic_X_MIN, dynamic_X_MAX)
local targetPosition = Vector3.new(randomPosX, 0, 0)
return targetPosition, camCF
end
