I’m currently working on a test dresser and I have it set up so whenever you click the dresser’s drawers they tween open like so:
https://gyazo.com/6229387d716f287800b4663283f3801b
but whenever the dresser is placed a different way it’ll tween in the wrong direction as such:
https://gyazo.com/832c55a5de2cd32935ac82379179742f
To combat this I made this little quick solution, but I’m wondering is there a better way to handle this because I don’t want to have to try to make this a thing for every single interactive item such ass doors etc.
local x
local y
local z
if Drawer.Orientation.Y == 0 then
x = 0
y = 0
z = 2
elseif Drawer.Orientation.Y == 90 then
x = 2
y = 0
z = 0
elseif Drawer.Orientation.Y == 180 then
x = 0
y = 0
z = -2
elseif Drawer.Orientation.Y == -90 then
x = -2
y = 0
z = 0
end
local openDrawer = {
Position = DrawerPos.Value + Vector3.new(x,y, z)
}
local closeDrawer = {
Position = DrawerPos.Value
}
local TweenOpen = TweenService:Create(Drawer, tweenInfo, openDrawer)
local TweenClose = TweenService:Create(Drawer, tweenInfo, closeDrawer)
Thank you for your help!
Use LookVector when determining the new position. Although since you didn’t start out using this, you might have to re-make the drawer. Since the LookVector might be to the side. This would also mean you will have to tween the drawer’s CFrame property, instead of the Position property.
e.g.
local openDrawer = {
CFrame = Drawer.CFrame.LookVector*(Insert_Number_Here) --you will have to do multiple trial runs to get this number right
It is simpler than you think. LookVector is a vector that faces the same way as the front of the part, and its length is always 1.
Insert_Number_Here is 2, because in OP’s code, the drawer is meant to be drawn out by 2 studs.
-- removed everything before these lines
local openDrawer = {
Position = DrawerPos.Value + Drawer.CFrame.LookVector * 2
}
local closeDrawer = {
Position = DrawerPos.Value
}
Pay attention to Drawer.CFrame.LookVector * 2.
It is exactly equivalent to the Orientation checks in the original post, and will in fact work even if the drawer is rotated in any direction.
If the Drawer.Orientation.Y == 0, then the LookVector is (0, 0, 1)
If the Drawer.Orientation.Y == 90, then the LookVector is (1, 0, 0)
If the Drawer.Orientation.Y == 45, then the LookVector is approximately (0.7071, 0, 0.7071) (0.7071 is half the square root of 2)
If your drawer’s front is facing toward the side or back of the drawer cabinet, then you may use CFrame.RightVector or CFrame.UpVector or the negative of any of the three vectors to get the correct direction without having to set up a dummy part/attachment solely for the right CFrame, or rebuild the drawer.
1 Like
Thank you so much, that quickly fixed my issue.