Help Needed With ViewportFrame Rotation Script

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! :slightly_smiling_face:

you could do the opposite of what happens when the mouse enters.

1 Like

I’m a bit confused on what you mean by this. When my mouse enters, I start rotating the camera. When my mouse leaves, I stop rotating the camera. Why would I stop rotating the camera when my mouse enters?

If you mean stop rotating the camera before I rotate it, I have tried that and it did not work.

clonedSample.MouseEnter:Connect(function()
    itemPreview:stop(item, camera)
	itemPreview:rotate(item, camera)
end)
			
clonedSample.MouseLeave:Connect(function()
	itemPreview:stop(item, camera)
end)

From what it looks like it is a detection issue. Using MouseEnter and MouseLeave is not the most effective way to do this. I would go about this by using MouseMoved and then checking if they left it or not.

vpf.MouseMoved:Connect(function()
	vpf.MouseEnter:Connect(function()
		itemPreview:rotate(item, camera)
	end)
	vpf.MouseLeave:Connect(function()
		itemPreview:stop(item, camera)
	end)
end)

Hope this helps!

1 Like

Thank you for replying! I tried this out and unfortunately got the same results. :slightly_frowning_face:

I also tried this and it didn’t fix the problem:

vpf.InputBegan:Connect(function(inputObject)
	if inputObject.UserInputType == Enum.UserInputType.MouseMovement then
		itemPreview:rotate(clonedPet, camera)
	end
end)
			
vpf.InputEnded:Connect(function(inputObject)
	if inputObject.UserInputType == Enum.UserInputType.MouseMovement then
		itemPreview:stop(clonedPet, camera)
	end
end)

I fixed the issue by adding game:GetService("RunService").RenderStepped:Wait() at the start of the function!

vpf.MouseEnter:Connect(function()
    game:GetService("RunService").RenderStepped:Wait()
	itemPreview:rotate(item, camera)
end)
			
vpf.MouseLeave:Connect(function()
    game:GetService("RunService").RenderStepped:Wait()
	itemPreview:stop(item, camera)
end)