Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

CFrame Rotation & Position?

I'm trying to make a Part slide open. But More like rotate on the side. I'm not sure how to explain this, but like trying to script a trash can lid, or a door open & close.

Would I Need to use both Rotation and changing position, or is there an easier method of achieving this?

1 answer

✓ Accepted answer

Yes you will need to both rotate and position it, as both of these are required to change when something swivels on a point other than it's centre. In this answer I will assume that you are using a single block as a door that you want to rotate about it's Y-axis on one of it's edges. However, it can be adapted quite easily for many purposes.

To accomplish what you want we need to know two things, the position of the pivot point and the offset from the centre of the object you are rotating. The offset can be calculated by taking the inverse of the CFrame obtained from the method of CFrame toObjectSpace from the object and pivot location.

local partCF = part.CFrame
local hingeCF = partCF*CFrame.new(-part.Size.X/2, 0, 0)
local offset = partCF:toObjectSpace(hingeCF):inverse()

Now that we have the offset established and our pivot location is known, we can set up a loop that will interpolate between the start and end positions. In this example, I will rotate a door 90 degrees about it's 'hinge':

local partCF = part.CFrame
local hingeCF = partCF*CFrame.new(-part.Size.X/2, 0, 0)
local offset = partCF:toObjectSpace(hingeCF):inverse()

local alpha = 0
local duration = 2
local elapsed = 0

while alpha < 1 do
	elapsed = elapsed + wait()
	alpha = math.min(1, elapsed/duration)
	
	local angle = alpha*90
	part.CFrame = hingeCF*CFrame.Angles(0, math.rad(angle), 0)*offset
end

Take a look at line 14. If we break this into steps, it will be easy to understand how it works. You start off by setting the part's CFrame to that of the hinges so that it is now centred at the hinge point. Then when you go rotate the part (which is what we do next) the part's axis of rotation is the same as the pivot's. After that, we multiply by the offset again to maintain the same offset between the part and the pivot that we had initially.

If you found any of this confusing, leave a comment and I'll try to clarify.

4 comments

0

elapsed = elapsed + wait() > Woah, never knew this was possible. What does it do?

0

The first return value of wait() is the amount of time it waited for. Therefore you can add this value to a variable to store a quite accurate value for the total elapsed time.  A better (but messier looking) way of doing this is using the difference in tick() returns to give you an exact elapsed time (especially useful when doing complicated computations inside the while loop that may take so

0

Wow! Thanks for all the help! This helped me so much

0

No problem :)

This discussion is preserved as it was. New answers, comments, and votes are closed.