Best way to smoothly move a model?

Im trying to make a model with multiple parts move along a straight line. Right now im moving it by constantly changing the cframe of the primarypart in a heartbeat loop, but this hogs memory. So what is the best way to do this?

4 Likes

This shouldn’t take significant amounts of memory.

What kind of model are you trying to move? I would recommend working with physics for large objects like spaceships/other vehicles with network ownership set to the client controlling the vehicle, or to use TweenService for all other use cases involving smoothly interpolating between values:
http://wiki.roblox.com/index.php?title=API:Class/TweenService

4 Likes

Tween service is probably the smoothest and easiest way to do it but there are some caveats. If you are running the tween from the server it might look a little jittery on slower connections.

If you want the smoothest thing possible not matter what it would probably be best to use body movers and/or other movement constraints because they are made to handle replication better.

1 Like

If you’re using SetPrimaryPartCFrame, then this can lead to the parts drifting away from each other after a lot of time.

Example

https://gfycat.com/IllfatedDisguisedBug

Code:

local model = workspace.Model

local angle = 0

while true do
	for i = 1, 100 do  -- repeat 100 time to exaggerate the effect so it can be seen in a 30 sec video
		model:SetPrimaryPartCFrame(CFrame.new(0, 10, 0)*CFrame.Angles(0, math.rad(angle), 0))
		angle = (angle + 0.1)%360
	end
	wait()
end


If you want to move this with physics, then you’ll want a joint structure like this

Root Part (Unanchored)
    Weld -> Model Parts (Unanchored)

If you want to move this with scripts and setting CFrame on Heartbeat, then you’ll want a joint structure like this

Root Part (Anchored)
    Motor6D -> Base Part (Unanchored)
        Weld -> Model Parts (Unanchored)

When you move the Root Part, all of the other parts will move too.

Key point with the latter one is that the Motor6D lets you move the anchored Root Part and have the rest of the unanchored parts move with it. If you use a Weld with an anchored Root instead of a Motor6D, then the Model Parts will not move when the Root does.



If you want this to look as smooth as possible to clients, you’ll need to either:

  • Use physics. Roblox makes this smooth automatically.
  • Set the CFrame on the client. This removes any latency or “network lag”, and has a higher update rate than the network send rate.
    • Optionally set the CFrame on RenderStepped. This will be “as smooth as possible” but isn’t necessary for it to be smooth.

Most of this response was taken from here.

24 Likes