I want to make a smooth walk animation using Interpolation but I don't know where to start writing my code. I've been wanting to learn interpolation for years and hopefully I went to the right site this time. Thank you very much.
Interpolation isn't all that helpful for player animations unless you're using custom players/rigs. If you're still using the default ROBLOX character rigs, either R6 or R15, it's much easier to use the Animation Editor and replacing the default Walk animation with your new one.
wiki article on using the Animation object in ROBLOX
Linear interpolation, or "lerp", is great for moving non-player objects. The built in CFrame:Lerp()
method is applied to the origin CFrame (or Vector3), takes the end position and interval as arguments, and returns the point between the beginning and end positions at the given interval.
The interval should be a fraction, with 0 representing the beginning point and 1 representing the end position.
Using this method combined with a simple loop, we can smoothly transition parts along these points.
Example code:
local part = script.Parent local EndPosition = CFrame.new(x, y, z) --Establish where we want the part to end. for _, i = 0, 1, 0.1 do --The 3rd number represents the rate at which the interval (i) is updated part.CFrame = part.CFrame:lerp(EndPosition, i) --CFrame:lerp() is called on the part's current position, and takes the EndPosition as its destination and i as its interval, with i increasing by 0.1 every 0.01 seconds. wait(.01) --The smaller the wait(), the "smoother" the lerp will look, but it will be much faster. end
This simple loop above moves the part from its current position to the EndPosition in about 0.1 seconds. We get this estimated time by dividing 1 by the interval amount, then multiplying it by the wait()
time. (Or 1 / 0.1 x 0.01 in this case)
Hope this helps you understand how :Lerp()
works. This also applies to Vector3 values as well, as both CFrame:lerp()
and Vector3:Lerp()
take the same arguments.