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

I know it is possible to tween GUI objects. How could I do the equivalent with parts?

I don't have any code because I simply don't know where I could start. I don't want code, I want ideas on how you would do this.

I know I could use a for loop to smoothly move the part but. is there a better way?

1 comment

0

Some scripts use a gui to tween the part, check out some free models, one by Kenetec which is good. It's exactly like tweening gui's, and uses a gui to tween. Learn from the free model

4 answers

✓ Accepted answer

####Adding on to lightpower26's answer:

There are a couple of different ways you can implement the interpolation loop, each of which has a different dependant variable.

###Depends on Iteration Count

This method is the same as lightpower26 suggested, where it changes purely based on the number of iterations:

local part = game.Workspace.Part
local StartCFrame = CFrame.new()
local EndCFrame = CFrame.new(5, 5, 5)

local alpha = 0
local iterations = 40

for i = 0, iterations do
	alpha = math.min(i/iterations)
	part.CFrame = StartCFrame:lerp(EndCFrame, alpha)
end

###Depends on Duration

This method is the one I most commonly use when doing interpolations as it will complete after a given duration I tell it:

local part = game.Workspace.Part
local StartCFrame = CFrame.new()
local EndCFrame = CFrame.new(5, 5, 5)

local alpha = 0
local duration = 40
local elapsed = 0

while elapsed < duration do
	elapsed = elapsed + wait()
	alpha = math.min(elapsed/duration, 1)
	part.CFrame = StartCFrame:lerp(EndCFrame, alpha)
end

###Depends on Speed

This method is another I use quite frequently, where you set a speed that the part will travel at during the interpolation:

local part = game.Workspace.Part
local StartCFrame = CFrame.new()
local EndCFrame = CFrame.new(5, 5, 5)

local alpha = 0
local speed = 20
local distance = (StartCFrame.p - EndCFrame.p).magnitude
local elapsed = 0

while alpha < 1 do
	elapsed = elapsed + wait()
	alpha = math.min((speed*elapsed)/distance, 1)
	part.CFrame = StartCFrame:lerp(EndCFrame, alpha)
end

Note: This is only a linear interpolation. If you want to have different easing styles you will need to implement them yourself. In order to manipulate the CFrames, you can interpolate the position vector but you'll need to covert the rotation to a quaternion and interpolate that.

1 comment

0

Thanks for the answer. These are all good answers but this gave the most understandable explanation.

I found a code snippet on the forum that could easily help. It uses the :lerp() function found in Vector3 and CFrame values. I couldn't understand it until I realized I needed a for loop. :lerp() takes in two arguements. The end position and the alpha. The alpha is a position in between the lerp. The larger the alpha, the more it moves to the end. Here's a wiki article about it

Here's what I have:

local StartCFrame = CFrame.new(0, 0, 0) --The position to start from
local EndCFrame = CFrame.new(5, 10, 0) --The position to end
local Alpha = 0.01 --Any number between 0 and 1
for i = 0, 1, Alpha do
	workspace.Part.CFrame = StartCFrame:lerp(EndCFrame, i)
	game["Run Service"].Heartbeat:wait() --Makes it move every frame instead of every server tick
end

I would turn this into a function and add arguments to it. Note that if you lerp the same part twice, you'll get this weird mirage until the first lerp ends. You might want to add a priority feature. I won't be doing all the work for you, since this isn't a request site. But at least you have the basics.

1 comment

0

I have used the lerp function. This is not ideal in some cases. The part you are moving slows down the closer it gets to it's destination.

Using BodyGyro, BodyForce, BodyVelocity? If you want to stick with CFrame loops, then i recommend you to use RenderStepped().

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