local vpf = script.Parent:WaitForChild("ViewportFrame")
local items = script.Parent:WaitForChild("Items")
local children = items:GetChildren()
local index, event = 1, nil
local camera = Instance.new("Camera")
camera.FieldOfView = 70
vpf.CurrentCamera = camera
vpf.Visible = true
-- rotation
local function setRotationEvent(model)
local currentAngle = 0
local modelCF, modelSize = model:GetBoundingBox()
local rotInv = (modelCF - modelCF.p):inverse()
modelCF = modelCF * rotInv
modelSize = rotInv * modelSize
modelSize = Vector3.new(math.abs(modelSize.x), math.abs(modelSize.y), math.abs(modelSize.z))
local diagonal = 0
local maxExtent = math.max(modelSize.x, modelSize.y, modelSize.z)
local tan = math.tan(math.rad(camera.FieldOfView/2))
if (maxExtent == modelSize.x) then
diagonal = math.sqrt(modelSize.y*modelSize.y + modelSize.z*modelSize.z)/2
elseif (maxExtent == modelSize.y) then
diagonal = math.sqrt(modelSize.x*modelSize.x + modelSize.z*modelSize.z)/2
else
diagonal = math.sqrt(modelSize.x*modelSize.x + modelSize.y*modelSize.y)/2
end
local minDist = (maxExtent/2)/tan + diagonal
return game:GetService("RunService").RenderStepped:Connect(function(dt)
currentAngle = currentAngle + 1*dt*60
camera.CFrame = modelCF * CFrame.fromEulerAnglesYXZ(0, math.rad(currentAngle), 0) * CFrame.new(0, 0, minDist + 3)
end)
end
local function setIndexActive(newIndex)
if (event) then
event:Disconnect()
children[index].Parent = items
end
index = newIndex
event = setRotationEvent(children[index])
children[index].Parent = vpf
end
--
setIndexActive(index)
vpf.InputBegan:Connect(function(input)
if (input.UserInputType == Enum.UserInputType.MouseButton1) then
setIndexActive(index + 1 > #children and 1 or index + 1)
end
end)
How would I make it so the object spins when the mouse is hovering over the ViewportFrame, and stops when it leaves it?