Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

How would you use MoveTo() to move models smoothly?

plane = script.Parent -- a model

plane:MoveTo(Vector3.new(-1574.794, 83.575, 0.726))

This script instantly moves the plane to the new location. By moving smoothly, I mean something like this that doesn't instantly teleport the plane:

brick = script.Parent -- a part

for i=1,100 do
	brick.Position = brick.Position + Vector3.new(1,1,1)
	wait(0.1)
end

How would you define the plane's Vector3 using MoveTo()?

1 comment

0

use lerp or tweenservice

2 answers

✓ Accepted answer

Unfortunately, TweenService does not work for models, because they have no way of setting their general position by property (TweenService tweens properties, so it can't tween a function like MoveTo).

This means we are left with psuedo-smooth ways of doing things - AKA, using MoveTo after waiting a very small time (this can be paired with lerp to script it quickly)

But, due to some more model limitations, you need to use PrimaryParts instead. To set a model's PrimaryPart property, click on it then click on the part you want to act as the model's 'center of movement'.

You can then use the functions GetPrimaryPartCFrame and SetPrimaryPartCFrame on the model to do it.

local Model = game.Workspace.Model
local Start,End = Model:GetPrimaryPartCFrame(), CFrame.new(Vector3.new(0,0,0))
-->> The latter is the target position and rotation

for Position = 0,1,0.01 do end
	-->> Increments by 0.01 every loop
	-->> The second value to pass to a lerp method is the 'position' in the lerp which is a value from 0-1, where 0 is the start and 1 is the end, and 0.5 is halfway to end

	local NewCFrame = Start:lerp(End,Position)
	Model:SetPrimaryPartCFrame(NewCFrame)
	wait()
end

To explain lerp a bit more, it is a way of getting the value in a given position with 2 other values to reference by (the value it is called on, and the first value).

Still doesn't really clear it up I think, check the wiki page on it ;)


Hope I helped!

~TDP

If you want to move it smoothly with a for loop, then you'll want to use a brick's position inside of it

for i = 1, 200 do -- moves model 20 studs
	local MoveMe = workspace.MoveMe
	MoveMe:MoveTo(MoveMe.Main.Position + Vector3.new(0,0,-.1))
	wait()
end

2 comments

0

how is that smooth

0

You could have easily made that much smoother...

This discussion is preserved as it was. New answers, comments, and votes are closed.