Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

Is there a way to smoothly CFrame a part from one spot to another?

Asked by
Teeter11 281 Moderation Voter
8 years ago

Does roblox have a built in method for cframing one part to another like tweenpositon but for cframes?

If not what would it look like to make my own?

Thanks!

2 answers

Log in to vote
3
Answered by 8 years ago

You can use the lerp method of a CFrame to return a CFrame that is interpolated between the given CFrame and the "goal" CFrame. You can use it like so:

CFrame:lerp(goal, alpha) --CFrame is the start CFrame, goal is the end CFrame, alpha is a number between 0 and 1 which tells the script how far to interpolate. If alpha was 0, the returned CFrame would be the start CFrame and vice versa.

How to apply it:

Using that on it's own will only return a CFrame based on the alpha given at that time. You would have to use a loop to smoothly tween from one CFrame to the other.

I like to do it this way:

local Sine = function(x) --Sine will be the variable for the function. Another way of saying function Sine(x)
    return math.sin(math.rad(x)) --Returns the sine of the angle x. This number will always be between 0 and 1.
end

function TweenPartCFrame(part,goal,duration,alpha) --part is the part you want to tween, goal is the end CFrame, duration is how long it takes and alpha is the Sine function we made earlier.
    local X = 0
    if part:FindFirstChild("TweenIndicator") then return end --If there is a tween happening, stop the function.
    local tweenIndicator = Instance.new("BoolValue",part) --Add value to the part to show it's being tweened.
    tweenIndicator.Name = "TweenIndicator"
    local start = part.CFrame --Variable of the start CFrame so we can use it to tween the part's CFrame properly.
    while true do
        local newX = X + math.min(1.5 / math.max(duration,0), 90)
        X = (newX > 90 and 90 or newX) --Sets X to 90 if newX is over 90, otherwise it's set to newX.
        part.CFrame = start:lerp(goal, alpha(X)) --Use lerp from the original CFrame to the goal CFrame with the value returned from the sine function.
        if X == 90 then break end --Breaks the loop if X is 90. We stop at 90 because the sine of a right angle will be 1 so we don't need to tween the part anymore.
        wait()
    end
    tweenIndicator:Destroy() --Destroy the value in the part to show we're not tweening it anymore.
end

--Example Usage:
TweenPartCFrame(workspace.Part,CFrame.new(),2,Sine) --Would tween the Part in workspace to CFrame.new(0,0,0) in two seconds.

I hope my answer helped you. If it did, be sure to accept it.

0
THANK YOU! Teeter11 281 — 8y
0
You're welcome. Spongocardo 1991 — 8y
Ad
Log in to vote
0
Answered by
pyro89 50
8 years ago

Well, if you want it to particularly move somewhere and be able to detect being touched, then you can try adding a velocity to the part. All you need to do is to set the velocity with Part.Velocity as a Vector3, then multiplying the Vector3 by how fast you want it to go in terms of guns.

Part.Velocity = Part.CFrame.lookVector * 150 -- This makes the part head forward at a speed of 150.

Otherwise, you can try doing a loop.

for i = 1, 50 do
    wait()
    Part.Position = Part.Position + Vector3.new(0, 1, 0) -- Makes the Part go 50 studs up.
end
0
not what im looking for Teeter11 281 — 8y

Answer this question