Issue
Whenever I move my mouse quickly from one ViewportFrame to another, the camera will not rotate around the part. However, If I move it slowly to another ViewportFrame, it will work.
Demonstration of moving mouse quickly:
Demonstration of moving mouse slowly:
Code
Here is the code that I use to rotate the model. Most of this code was written by EgoMoose, I simply just modified it to work with my system.
This code is in a ModuleScript.
local event = nil
function itemPreview:setRotationEvent(model, camera)
local currentAngle = restingAngle
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/.7))
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 * 100
camera.CFrame = modelCF * CFrame.fromEulerAnglesYXZ(-.4, math.rad(currentAngle), 0) * CFrame.new(0, 0, minDist + 3)
end)
end
function itemPreview:rotate(model, camera)
if (event) then
event:Disconnect()
end
event = itemPreview:setRotationEvent(model, camera)
end
function itemPreview:stop(model, camera)
if (event) then
event:Disconnect()
camera.CFrame = itemPreview:getRestingPosition(camera, model)
end
end
This code is in a LocalScript.
vpf.MouseEnter:Connect(function()
itemPreview:rotate(item, camera)
end)
vpf.MouseLeave:Connect(function()
itemPreview:stop(item, camera)
end)
Resources
Original code for the camera rotation script: How to make camera rotate around model in ViewPort Frame while mantaining set distance and angles - #8 by EgoMoose
A reply I made on the post asking about the issue: How to make camera rotate around model in ViewPort Frame while mantaining set distance and angles - #21 by 0929lego
Any help would be appreciated, thank you!