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

How does one go about making a part face the direction of movement using CFrame Tweening?

Asked by 4 years ago

What I intend to do, is make a model with a RootPart, that will turn, start moving towards a point/part, while retaining its rotation, and then stop at the destination without rotating. Here's the code that I've got so far:

local tweenservice = game:GetService("TweenService")
local RootPart = script.Parent.RootPart
local target = workspace.Target

-- // Config \\ --
local SPS = 3



function move()
    local velocity = (RootPart.Position - target.Position).Magnitude / SPS
    local orient = CFrame.new(script.Parent.RootPart.Position,target.Position)
    local Orientation = {CFrame = orient}
    local dest = CFrame.new(target.Position.X,RootPart.Position.Y,target.Position.Z) * CFrame.Angles(0,0,0)
    local Destination = {CFrame = dest}
    local move = tweenservice:Create(RootPart,TweenInfo.new(velocity,Enum.EasingStyle.Sine),Destination)
    local rotate = tweenservice:Create(RootPart,TweenInfo.new(1,Enum.EasingStyle.Sine),Orientation)
    rotate:Play()
    rotate.Completed:Connect(function()
        local newMatrices = RootPart.CFrame:GetComponents()
    wait(.1)
    move:Play()
    end)
end

local debounce = false
workspace.Target.Changed:Connect(function()
    if not debounce then
        debounce = true
        print("moving")
        wait(2)
        move()
        debounce = false
    end
end)

It works right up until the model starts moving, and the RootPart's orientation starts tweening towards 0,0,0.

I suspect this has to do with the fact since I am not setting the part's rotational CFrame, it resets it back to 0,0,0, however I do not know how I would go about tweening it in a way where it either keeps the rotation, or it doesn't utilise the rotation parameters.

Any help and resources are greatly appreciated!

1 answer

Log in to vote
0
Answered by 4 years ago

I've thankfully gotten an answer after searching for 6 days from the good folk on discord! Thank you Dandystan#0001 and megukoo#0013!

Here's my solution if anyone else is struggling!

local tweenservice = game:GetService("TweenService")
local RootPart = script.Parent.RootPart
local target = workspace.Target

-- // Config \\ --
local SPS = 3



function move()
    local velocity = (RootPart.Position - target.Position).Magnitude / SPS
    local Orientation = {CFrame = CFrame.new(script.Parent.RootPart.Position,target.Position)}
    local rotate = tweenservice:Create(RootPart,TweenInfo.new(1,Enum.EasingStyle.Sine),Orientation)
    rotate:Play()
    rotate.Completed:Connect(function()
        wait(.1)
        local oldCframe = RootPart.CFrame
        local Destination = {CFrame = oldCframe - oldCframe.Position + Vector3.new(target.Position.X,RootPart.Position.Y,target.Position.Z)}
        local move = tweenservice:Create(RootPart,TweenInfo.new(velocity,Enum.EasingStyle.Sine),Destination)
        move:Play()
    end)
end

local debounce = false
workspace.Target.Changed:Connect(function()
    if not debounce then
        debounce = true
        print("moving")
        wait(2)
        move()
        debounce = false
    end
end)

Ad

Answer this question