I'm making a sliding door, and I am trying to make it stay the correct way when it does the sliding. What happens at the moment is that it rotates 90 degrees to its original rotation, because I haven't set a rotation I think.
for i=1, b do script.Parent.CFrame = CFrame.new(script.Parent.Position.X,script.Parent.Position.Y,script.Parent.Position.Z + -0.12) script.Parent.CFrame = CFrame.fromEulerAnglesXYZ(0, 90, 0) wait(0.03) end
I need to know the way of rotating a part, can anyone help me? Also,the b
value is defined, so that's not a problem.
Hello AkaLua,
Basically, you're using CFrame.fromEulerAnglesXYZ()
to rotate the object about the y-axis by 90 degrees. This method can be used as an alternative to CFrame.Angles
, another way to rotate objects by inputting the rotation increment in radians, or usingmath.rad()
. For example, CFrame.Angles(0,math.rad(90),0)
. The first method, however, can be used to find more information, such as heading, attitude, etc. about your rotating object.
Therefore, if you want your door to slide you can create a motion without defining each variable of x, y, and z, and just call out the position in general:
for i=1,b do wait(0) script.Parent.CFrame= script.Parent.CFrame*CFrame.new(1,0,0) end
In your case, it would be:
for i=1,b do script.Parent.CFrame= script.Parent.CFrame*CFrame.new(0,0,-0.12) wait(0.03) end
So, script.Parent.CFrame
is your new position of your target object, and you're running a recursive statement, as the position of the object changes by a stud in the positive x-direction for every step of wait(), CFrame.new(1,0,0)
(for the first script) added on to the previous location of the object.
If you want to rotate back, you can add 270 degrees as that would make 360 degrees/ back to the original position, or subtract 90 by switching the sign to negative:
for i=1,b do script.Parent.CFrame= script.Parent.CFrame*CFrame.new(0,0,-0.12) script.Parent.CFrame = script.Parent.CFrame*CFrame.fromEulerAnglesXYZ(0, 90, 0) script.Parent.CFrame = script.Parent.CFrame*CFrame.fromEulerAnglesXYZ(0,- 90, 0) wait(0.03) end
Finally, it's always a good idea to check for recursion, as it is widely used for objects in step events which allows the user to analyze the progression of a "moving object" and it is easier to edit how it works/ functions.