How to add to CFrame and not Set

I’ve created a 3D view of a model using viewport frames and I tried to get it to rotate when you hold down your mouse and move. I got it to work but if you let go and do it again hold-clicking in a different position, it ‘snaps’ back.

https://gyazo.com/4fada78915cbb80161ca8b1ca5ea4fd6

What I want it to do is when I move my mouse it adds to the rotation and not set it. Like no matter where I decide to click-hold-drag it won’t snap, it will just continue where it left off.

Code:
local x,y = Interface.AbsoluteSize.X / 2 - Mouse.X,Interface.AbsoluteSize.Y / 2 - Mouse.Y; local NewCFrame = modelCF * CFrame.fromEulerAnglesYXZ(math.rad(y),math.rad(x), 0) * CFrame.new(0, 10, minDist + 10) TweenService:Create(ViewCamera,TweenInfo.new(0.15),{CFrame=NewCFrame}):Play()

2 Likes

Do it as a delta i.e. keep track of your current x, y coordinates and then transform them by the new mouse coordinates

I don’t follow, could you elaborate more?

local coords; coords = { --> Starter coords
	x = 0;
	y = 0;
	push = (function (obj)
		coords.x, coords.y = obj.X - coords.x, obj.Y - coords.y
	end);
}


--> When players clicks etc. let's update with current mouse coordinates
coords.push(mouse)

--> Make the changes
local x, y = Interface.AbsoluteSize.X / 2 - coords.x,Interface.AbsoluteSize.Y / 2 - coords.y
local NewFrame = modelCF * CFrame.fromEulerAnglesYXZ(math.rad(x), math.rad(y), 0) * CFrame.new(0, 10, minDist + 10)
TweenService:Create(ViewCamera,TweenInfo.new(0.15),{CFrame=NewCFrame}):Play()

Something like the above

1 Like

You could set a variable which remembers the CFrame from when you stopped interaction, so once you start moving it again you can use the CFrame to prevent it from snapping back. This allows you to set from a certain point rather than directly adding which could be catastrophic in this scenario.

Edit: Using mouse deltas also works.

After some rest and some thinking, I understood what I needed to do, thanks for your help!