How to make my grid placement system lerp preview model CFrames

As the player moves his/her mouse over their plot, a preview model hovers with the mouse. Instead of snapping abruptly to the grid, I would like it to almost lerp to its new calculated position as shown (Miner’s Haven):


Here is my current code for calculating model preview CFrame:

function Placement:CalcPlacementCFrame(model, position, rotation)

	local cf, size = self:CalcCanvas()
	model.PrimaryPart = model.GridPart

	local modelSize = CFrame.fromEulerAnglesYXZ(0, rotation, 0) * model.PrimaryPart.Size
	modelSize = Vector3.new(math.abs(modelSize.x), math.abs(modelSize.y), math.abs(modelSize.z))

	local lpos = cf:pointToObjectSpace(position);
	local size2 = (size - Vector2.new(modelSize.x, modelSize.z))/2

	local x = math.clamp(lpos.x, -size2.x, size2.x);
	local y = math.clamp(lpos.y, -size2.y, size2.y);
	
	local g = self.GridUnit
	
	if (g > 0) then
		x = math.sign(x)*((math.abs(x) - math.abs(x) % g) + (size2.x % g))
		y = math.sign(y)*((math.abs(y) - math.abs(y) % g) + (size2.y % g))
	end

	return cf * CFrame.new(x, y, -modelSize.y/2) * CFrame.Angles(-math.pi/2, rotation, 0)
	
end

And the return value of this method is used on the client in a way as such:

	local cf = CanvasTool:CalcPlacementCFrame(part, mouse.Hit.p, rotation)
	part:SetPrimaryPartCFrame(cf)

Any feedback or solutions is greatly appreciated

Instead of using SetPrimaryPartCFrame, try welding all the model’s parts to the root part (and unanchor them), then you can use TweenService to animate the root part directly and all its parts will follow.

You can have a goal CFrame and just Lerp the current CFrame towards that goal CFrame.
Some example:

local goalCFrame = ...;
model:SetPrimaryPartCFrame(model:GetPrimaryPartCFrame():Lerp(goalCFrame, ...))
1 Like

Is this solution good memory wise ? Would creating a new tween every frame cause any performance issues ?

It shouldn’t if it’s done locally (which I’d assume would be the case since the client is figuring out where to position it)

You’re just animating the CFrame property of the model’s root part, and then because of it being welded with everything else, the other parts will follow.

Wouldn’t using CFrames use less memory than Tweening ? As discussed in this post:

I’ve never had a performance issue with it. You’re tweening just one model too. I’ve relied on tweening for animations for game objects in one of my games, and it worked out great.