How to fix the stuttering on lerped model movement

I’m trying to lerp a model to my mouse but it keeps stuttering and delaying…

My code:

local function lerpModel()
	for i = 0, 1, 0.05 do
		local pos = Vector3.new(mouse.Hit.X,mouse.Hit.Y,mouse.Hit.Z)
		wait()
		placingModel:MoveTo(pos:lerp(Vector3.new(math.ceil(mouse.Hit.x), mouse.Hit.y, math.ceil(mouse.Hit.z)), i))
		--placingModel:MoveTo(Vector3.new(math.ceil(mouse.Hit.x), mouse.Hit.y, math.ceil(mouse.Hit.z)))
	end
end
mouse.Move:Connect(function()
	if mouse.Target then
		if mouse.Target == plot and mouse.TargetSurface == Enum.NormalId.Top then
			wait()
			lerpModel()
		end
	end
end)

VIDEO:

Gone to eat currently, cant reply for a bit. :slight_smile:

2 Likes

You should use Mouse.TargetFilter = the model

You should also try just positioning only, not with lerping

1 Like

Maybe it’s because multiple loops are running at once. You could use break on old loops to prevent that.

1 Like

I have used positioning, but I like the smoother lerp effect.

How would I put this into place?

It’s also probably because :MoveTo uses collisions, and there will be collisions between the ground and the chair

Don’t use :MoveTo for something like this, just use normal cframe with lerp (SetPrimaryPartCFrame) or use body movers as said by Alkan below.

Have a variable local changed = false, and in the for loop, have if changed then break end. Also, for the mouse.Move connection, after wait(), have a check: if not changed then changed = true end. Finally, make another connection: mouse.Idle:Connect(function() changed = false end). If you implement all this, it should work.

You are running the move function, which lasts 20 frames, every frame. There is your issue.

You can try using body mover instances to get the smooth movement I imagine you were trying to do, or reposition the model a fraction of the way every frame, though that sounds a little clunky.

1 Like

Relevant: SetPrimaryPartCFrame

Also, use TweenService. Don’t animate lerps manually.

1 Like

What if i used a while loop? It wouldnt matter the players FPS?

It should be fine. In fact preferable to mouse.move, because that wont fire if you move the camera but not the mouse.

Alright, so all I’d have to do is wait one second ad thats the time it takes to lerp, correct?

Depends on the way you want to do this. I recommend using body instances, which should not require any lerping.

How would I go about that with my mouse, the item constantly having to move, and my model is anchored?

Unanchor the model, put the body movers in the primary part of the model(usually an invisible part at the base of the mode). Every frame or two you set the body mover position to the mouse position

I’m using a BodyPosition and absolutely nothing happens except my Entire model falls apart and through the baseplate… It’s cancollide off if thats important.

local function positionModel()
	placingModel.Hitbox.BodyPosition.Position = Vector3.new(mouse.Hit.X, mouse.Hit.Y, mouse.Hit.Z)
end
----------- [ EVENTS ] -------------
while placing == true do
	wait()
	if mouse.Target then
		if mouse.Target == plot and mouse.TargetSurface == Enum.NormalId.Top then
			wait()
			positionModel()
		end
	end
end

Cancollide should be off, but youll also want to weld every part in the mode to the primary part