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

CFrame.lookVector not working as expected?

Asked by
AZDev 590 Moderation Voter
9 years ago

The code is supposed to open a drawer inside a nightstand. When the Model is rotated at (0,0,0) the code works as intended. When I rotate the model to (0, 90, 0) the code rotates the Drawer back to (0, 0, 0)

I need the model to be able to be place anywhere on a map and be rotated at any angle and still function as intended. I plan on reusing this model across the map and do not want to have to change the position for every Drawer.

script.Parent.DrawerOpen.Value = false

script.Parent.ClickDetector.MouseClick:connect(function(OpenDrawer)
    if script.Parent.DrawerOpen.Value == false then
        script.Parent.CFrame = CFrame.new(script.Parent.Position + (script.Parent.CFrame.lookVector * -1))
        script.Parent.DrawerOpen.Value = true
        else if script.Parent.DrawerOpen.Value == true then
            script.Parent.CFrame = CFrame.new(script.Parent.Position + (script.Parent.CFrame.lookVector * 1))
            script.Parent.DrawerOpen.Value = false
        end
    end
end)

Thanks!

1 answer

Log in to vote
0
Answered by
Perci1 4988 Trusted Moderation Voter Community Moderator
9 years ago

The problem is that you're using position. Position doesn't include rotation. Therefore your part gets rotated back to 0, 0, 0.

CFrame includes rotation. Therefore adding to the part's CFrame will let rotation be maintained.

part.CFrame = part.CFrame * CFrame.new(1, 0, 0)

You may wait to edit the Z (instead of X) depending which way you made the 'front' of the drawer.

Ad

Answer this question