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

How can I preserve orientation of my model while moving it with TweenService?

Asked by 4 years ago

I'm building a set of sliding doors that use TweenService when I press a button to trigger them. There are two positions (opened and closed) that are represented using four invisible, massless and non-CanCollide bricks, which are identical to the real doors. (Door1Closed, Door1Opened, Door2Closed and Door2Opened)

Both doors are models, so I built a function ('tweenModel' in the code) to tween every part in the model independently rather than using SetPrimaryPartCFrame(), because it caused trouble with special welds elsewhere in my game.

It works, but it also tweens to an orientation of {0, 0, 0}. Somebody mentioned that the problem stems from CFrame including orientation values as well as position, and me calculating CFrame from Vector3 (Position), gives it no values for orientation, causing it to resort to the default (which I assume is {0, 0, 0})

If this is the case, then how can I keep the orientation of my doors while using TweenService to translate them?

Here's a video of the doors: https://imgur.com/qjWnSsO

Here's the script:

local TweenService = game:GetService("TweenService")
local Door1 = script.Parent:WaitForChild("Door1")
local Door2 = script.Parent:WaitForChild("Door2")
local Targets = script.Parent.Targets
local tweeningInformation = TweenInfo.new(
    1,
    Enum.EasingStyle.Bounce,
    Enum.EasingDirection.Out,
    0,
    false,
    0
)

local Open = true
local OpenButton = script.Parent:WaitForChild("OpenButton")
local CloseButton = script.Parent:WaitForChild("CloseButton")

function tweenModel(model, tweeningInfo, vector)
    local descendants = model:GetDescendants()
    for i=1, #descendants do
        if descendants[i]:IsA("BasePart") then
            print(descendants[i])
            print(vector)
            local modVector = {CFrame = CFrame.new(descendants[i].Position + vector)}
            print(modVector)
            local temp = TweenService:Create(descendants[i], tweeningInfo, modVector)
            temp:Play()
        end
    end
end

function operate()
    if Open == true then
        CloseButton.ClickDetector.MaxActivationDistance = 0
        CloseButton.BrickColor = BrickColor.new("Neon orange")
        print(Targets.Door1Closed.Position - Targets.Door1Opened.Position)
        tweenModel(Door1, tweeningInformation, (Targets.Door1Closed.Position - Targets.Door1Opened.Position))
        tweenModel(Door2, tweeningInformation, (Targets.Door2Closed.Position - Targets.Door2Opened.Position))
        wait(1)
        CloseButton.BrickColor = BrickColor.new("Crimson")
        OpenButton.BrickColor = BrickColor.new("Electric blue")
        OpenButton.ClickDetector.MaxActivationDistance = 32
        Open = not Open
    else
        OpenButton.ClickDetector.MaxActivationDistance = 0
        OpenButton.BrickColor = BrickColor.new("Neon orange")
        tweenModel(Door1, tweeningInformation, (Targets.Door1Opened.Position - Targets.Door1Closed.Position))
        tweenModel(Door2, tweeningInformation, (Targets.Door2Opened.Position - Targets.Door2Closed.Position))
        wait(1)
        OpenButton.BrickColor = BrickColor.new("Crimson")
        CloseButton.BrickColor = BrickColor.new("Electric blue")
        CloseButton.ClickDetector.MaxActivationDistance = 32
        Open = not Open
    end
end

OpenButton.ClickDetector.MouseClick:Connect(operate)
CloseButton.ClickDetector.MouseClick:Connect(operate)

1 answer

Log in to vote
1
Answered by 4 years ago
Edited 4 years ago

If you want to keep the orientation, you can simply replace the CFrame field in the properties table with Position so the third argument when you are creating the tween would be something like {Position = descendants[i].Position + vector}

0
Thanks, it works perfectly now! nikoviking 236 — 4y
Ad

Answer this question