Rotating CFrame after positioning CFrame?

8e5ef3b53267f770632e0cbe792d1799

In the gif above, you can see the view model rotating with the direction the mouse is moving. The issue is, I am trying to get the model to rotate about the red highlighted part. I have tried using a combination of CFrame:ToWorldSpace() and CFrame:ToObjectSpace() to apply this rotation each frame, but so far I have been unlucky in my attempts. This is the code I have below:

--calculate gunroot offset
		local gunRootOffset = RootPart.CFrame:ToObjectSpace(gunRoot.WorldCFrame)
		
		--swivel towards mouse delta
		local delta = UIS:GetMouseDelta()
		local deltaOffset = gunRootOffset * CFrame.Angles(math.clamp(-delta.Y*.01, -.05, .05), math.clamp(-delta.X*.01, -.05, .05), 0)
		swivelOffset = RootPart.CFrame:ToObjectSpace( RootPart.CFrame
			* gunRootOffset:Inverse() * 
			deltaOffset )
		
		
		--position set by other scripts, represents base camera position to be modified
		local cPos = cameraDataModule.Pos
		
		cPos *= swivelOffset
		
		--position viewmodel
		viewModel.PrimaryPart.CFrame = cPos

Are you trying to set the viewmodel offset point? If so you can do it by setting the BasePart.PivotOffset (through a script, or straight from the explorer properties) and then change the CFrame by using BasePart:PivotTo()

Well the issue with this is that there are actually two different rotations happening with the viewmodel here:

  • rotation to follow camera (works fine in video)
  • rotation to follow camera movement (needs to pivot around red part)

If I change the pivot point, the second one would work just fine. As a tradeoff though, the first one would then no longer work as intended.