How do I keep this object's CFrame relative to the camera? (Even when tweening)

So I have been trying to create a 3D document inspection/read system and it all seems to work pretty well until you start to move your camera or character around.

A solution I have tried is using a ViewportFrame, which works pretty well, but the rendered decal is way too low quality.

My current methods for moving the object around is with CFrame and TweenService. If I wasn’t using tweening I could easily fix this issue, but I am using tweening as a little animation for the object, which causes issues as seen below.

Does anyone know a solution or an alternative solution to this problem?

Help will be greatly appreciated as always!


local function InsertObject(ObjectToView)	
	CurrentObject = ObjectToView:Clone()
	CurrentObject.CanCollide = false
	CurrentObject.CFrame = PlayerCamera.CFrame * ObjectToView.ViewAttachment.CFrame * ViewStartCFrame
	CurrentObject.Parent = PlayerCamera
end

Main pickup/putdown functions with tweening

local function Open(SelectedPaper)
	if not Toggling then
		Toggling = true
		print("Opening paper...")
		CloseButton.Visible = true
		CloseButton.Modal = true
		BackgroundBlur.Enabled = true
		
		Mouse.Icon = ""

		InsertObject(SelectedPaper)

		local ObjectTween = TweenService:Create(CurrentObject, PaperTweenInfo, {CFrame = PlayerCamera.CFrame * CurrentObject.ViewAttachment.CFrame})
		local BlurTween = TweenService:Create(BackgroundBlur, PaperTweenInfo, {FarIntensity = 1})
		ObjectTween:Play()
		BlurTween:Play()
	
		OpenSound:Play()
		
		wait(TweenTime)
		Toggling = false
	end
end

local function Close()
	if CurrentObject and not Toggling then
		Toggling = true
		print("Closing paper...")
		CloseButton.Modal = false
		CloseButton.Visible = false
		
		Mouse.Icon = OriginalMouseIcon

		local ObjectTween = TweenService:Create(CurrentObject, PaperTweenInfo, {CFrame = PlayerCamera.CFrame * CurrentObject.ViewAttachment.CFrame * ViewEndCFrame})
		local BlurTween = TweenService:Create(BackgroundBlur, PaperTweenInfo, {FarIntensity = 0})
		ObjectTween:Play()
		BlurTween:Play()

		wait(TweenTime)

		BackgroundBlur.Enabled = false
		
		if CurrentObject then
			CurrentObject:Destroy()
			CurrentObject = nil
		end
		
		Toggling = false
	end
end
2 Likes

One solution I can think of is once you have tweened the object to the players camera you can use a RenderStepped function to change the objects CFrame every frame to the players camera * an offset. So possibly something like this:

local runService = game:GetService("RunService")

local attachObjectToCamera = runService.RenderStepped:Connect(function()
	CurrentObject.CFrame = PlayerCamera.CFrame * CurrentObject.ViewAttachment.CFrame
end)

Once you need to close the paper, you can do

attachObjectToCamera:Disconnect()

And then run your tween to close the paper

For this, id recommend doing the tween on a CFrameValue that has the CFrame zerod. Aka just pretending 0,0,0 is the camera cframe. Then twern the CFrameValue to the respective offset (once again, assuming 0,0,0 is the camera cframe) then every frame apply the books cframe to the camera’s cframe * the cframevalue. This will still tween the book as normal but it will still remain camera relative even while it’s moving.

2 Likes

Thanks. This method turned out working perfectly!